⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 waveout.cs

📁 用C#实现的取得CellID和LAC的程序源代码!
💻 CS
📖 第 1 页 / 共 2 页
字号:

		/// <summary>
		/// Specifies if playback has finished.
		/// </summary>
		/// <returns>true if no playback is in progress.</returns>
		public bool Done()
		{
			if (m_file != null)
				return m_file.Done;

			return true;
		}

		/// <summary>
		/// Pause playback.
		/// </summary>
		/// <returns>MMSYSERR.NOERROR if successful</returns>
		public Wave.MMSYSERR Pause()
		{
			if (m_file != null)
				return m_file.Pause();

			return Wave.MMSYSERR.NOERROR;
		}

		/// <summary>
		/// Resume playback of a paused sound.
		/// </summary>
		/// <returns>MMSYSERR.NOERROR if successful</returns>
		public Wave.MMSYSERR Resume()
		{
			if (m_file != null)
				return m_file.Resume();

			return Wave.MMSYSERR.NOERROR;
		}

		/// <summary>
		/// Determines the length of the audio file, in milliseconds.
		/// </summary>
		/// <returns>Millieconds</returns>
		public uint Milliseconds()
		{
			if (m_file != null)
				return m_file.Milliseconds;

			return 0;
		}

		/// <summary>
		/// Play a file.
		/// </summary>
		/// <param name="fileName">Name of file to play</param>
		/// <param name="bufferSize">Size of playback buffers</param>
		/// <param name="volLeft">Volume of left channel</param>
		/// <param name="volRight">Volume of right channel</param>
		/// <returns>MMSYSERR.NOERROR if successful</returns>
		public Wave.MMSYSERR Play(string fileName, int bufferSize, ushort volLeft, ushort volRight)
		{
			if (m_file != null)
				return m_file.Play(0, fileName, m_msgWindow.Hwnd, bufferSize, volLeft, volRight);

			return Wave.MMSYSERR.ERROR;
		}

		/// <summary>
		/// A block has finished so notify the WaveFile.  In a more complicated example,
		/// this class might maintain an array of WaveFile instances.  In such a case, the
		/// wParam of the message could be passed from the MM_WIM_DATA message.  This
		/// value represents the m_hwi member of the file that caused the message.
		/// The code might look something like:
		/// foreach (WaveFile f in m_files)
		/// {
		///		if (f.m_hwi.ToInt32() == wParam.ToInt32())
		///		{
		///			f.BlockDone();
		///			break;
		///		}
		/// }
		/// </summary>
		public void BlockDone(IntPtr hwo)
		{
			if (m_file != null)
				m_file.BlockDone();
		}

		/// <summary>
		/// Clean up an allocated resources.
		/// </summary>
		public void Dispose()
		{
			if (m_msgWindow != null)
				m_msgWindow.Dispose();

			if (m_file != null)
				m_file.Dispose();
		}

		/// <summary>
		/// This function retrieves the number of waveform output devices present
		/// in the system.
		/// </summary>
		/// <returns>The number of devices indicates success. Zero indicates that
		/// no devices are present or that an error occurred.</returns>
		[DllImport ("coredll.dll")]
		protected static extern int waveOutGetNumDevs();

		/// <summary>
		/// This function opens a specified waveform output device for playback.
		/// </summary>
		/// <param name="phwo">Address filled with a handle identifying the open
		/// waveform-audio output device. Use the handle to identify the device
		/// when calling other waveform-audio output functions. This parameter might
		/// be NULL if the WAVE_FORMAT_QUERY flag is specified for fdwOpen.</param>
		/// <param name="uDeviceID">Identifier of the waveform-audio output device to
		/// open. It can be either a device identifier or a Handle to an open
		/// waveform-audio input device.</param>
		/// <param name="pwfx">Pointer to a WAVEFORMATEX structure that identifies
		/// the format of the waveform-audio data to be sent to the device. You can
		/// free this structure immediately after passing it to waveOutOpen.</param>
		/// <param name="dwCallback">Specifies the address of a fixed callback function,
		/// an event handle, a handle to a window, or the identifier of a thread to be
		/// called during waveform-audio playback to process messages related to the
		/// progress of the playback. If no callback function is required, this value
		/// can be zero.</param>
		/// <param name="dwInstance">Specifies user-instance data passed to the
		/// callback mechanism. This parameter is not used with the window callback
		/// mechanism.</param>
		/// <param name="fdwOpen">Flags for opening the device.</param>
		/// <returns>MMSYSERR</returns>
		[DllImport ("coredll.dll")]
		private static extern Wave.MMSYSERR waveOutOpen(ref IntPtr phwo, uint uDeviceID, Wave.WAVEFORMATEX pwfx, IntPtr dwCallback, uint dwInstance, uint fdwOpen);

		/// <summary>
		/// This function queries the current volume setting of a waveform output device.
		/// </summary>
		/// <param name="hwo">Handle to an open waveform-audio output device. This
		/// parameter can also be a device identifier.</param>
		/// <param name="pdwVolume">Pointer to a variable to be filled with the current
		/// volume setting. The low-order word of this location contains the left-channel
		/// volume setting, and the high-order word contains the right-channel setting.
		/// A value of 0xFFFF represents full volume, and a value of 0x0000 is silence. 
		/// If a device does not support both left and right volume control, the low-order
		/// word of the specified location contains the mono volume level.
		/// The full 16-bit setting(s) set with the waveOutSetVolume function is returned,
		/// regardless of whether the device supports the full 16 bits of volume-level
		/// control.</param>
		/// <returns>MMSYSERR</returns>
		[DllImport ("coredll.dll")]
		protected static extern Wave.MMSYSERR waveOutGetVolume(IntPtr hwo, ref uint pdwVolume);

		/// <summary>
		/// This function sets the volume of a waveform output device.
		/// </summary>
		/// <param name="hwo">Handle to an open waveform-audio output device. This
		/// parameter can also be a device identifier.</param>
		/// <param name="dwVolume">Specifies a new volume setting. The low-order word
		/// contains the left-channel volume setting, and the high-order word contains
		/// the right-channel setting. A value of 0xFFFF represents full volume, and a
		/// value of 0x0000 is silence. 
		/// If a device does not support both left and right volume control, the low-
		/// order word of dwVolume specifies the volume level, and the high-order word
		/// is ignored.</param>
		/// <returns>MMSYSERR</returns>
		[DllImport ("coredll.dll")]
		protected static extern Wave.MMSYSERR waveOutSetVolume(IntPtr hwo, uint dwVolume);

		/// <summary>
		/// This function prepares a waveform data block for playback.
		/// </summary>
		/// <param name="hwo">Handle to the waveform-audio output device.</param>
		/// <param name="pwh">Pointer to a WAVEHDR structure that identifies the data
		/// block to be prepared. The buffer's base address must be aligned with the
		/// respect to the sample size.</param>
		/// <param name="cbwh">Size, in bytes, of the WAVEHDR structure.</param>
		/// <returns>MMSYSERR</returns>
		[DllImport ("coredll.dll")]
		private static extern Wave.MMSYSERR waveOutPrepareHeader(IntPtr hwo, Wave.WAVEHDR pwh, uint cbwh);

		/// <summary>
		/// This function sends a data block to the specified waveform output device.
		/// </summary>
		/// <param name="hwo">Handle to the waveform-audio output device.</param>
		/// <param name="pwh">Pointer to a WAVEHDR structure containing information
		/// about the data block.</param>
		/// <param name="cbwh">Size, in bytes, of the WAVEHDR structure.</param>
		/// <returns>MMSYSERR</returns>
		[DllImport ("coredll.dll")]
		private static extern Wave.MMSYSERR waveOutWrite(IntPtr hwo, Wave.WAVEHDR pwh, uint cbwh);

		/// <summary>
		/// This function cleans up the preparation performed by waveOutPrepareHeader.
		/// The function must be called after the device driver is finished with a data
		/// block. You must call this function before freeing the data buffer.
		/// </summary>
		/// <param name="hwo">Handle to the waveform-audio output device.</param>
		/// <param name="pwh">Pointer to a WAVEHDR structure identifying the data block
		/// to be cleaned up.</param>
		/// <param name="cbwh">Size, in bytes, of the WAVEHDR structure.</param>
		/// <returns>MMSYSERR</returns>
		[DllImport ("coredll.dll")]
		private static extern Wave.MMSYSERR waveOutUnprepareHeader(IntPtr hwo, Wave.WAVEHDR pwh, uint cbwh);

		/// <summary>
		/// This function closes the specified waveform output device.
		/// </summary>
		/// <param name="hwo">Handle to the waveform-audio output device. If the function
		/// succeeds, the handle is no longer valid after this call.</param>
		/// <returns>MMSYSERR</returns>
		[DllImport ("coredll.dll")]
		protected static extern Wave.MMSYSERR waveOutClose(IntPtr hwo);

		/// <summary>
		/// This function stops playback on a specified waveform output device and
		/// resets the current position to 0. All pending playback buffers are marked
		/// as done and returned to the application.
		/// </summary>
		/// <param name="hwo">Handle to the waveform-audio output device.</param>
		/// <returns>MMSYSERR</returns>
		[DllImport ("coredll.dll")]
		protected static extern Wave.MMSYSERR waveOutReset(IntPtr hwo); 

		/// <summary>
		/// This function pauses playback on a specified waveform output device. The
		/// current playback position is saved. Use waveOutRestart to resume playback
		/// from the current playback position.
		/// </summary>
		/// <param name="hwo">Handle to the waveform-audio output device.</param>
		/// <returns>MMSYSERR</returns>
		[DllImport ("coredll.dll")]
		protected static extern Wave.MMSYSERR waveOutPause(IntPtr hwo);
 
		/// <summary>
		/// This function restarts a paused waveform output device.
		/// </summary>
		/// <param name="hwo">Handle to the waveform-audio output device.</param>
		/// <returns>MMSYSERR</returns>
		[DllImport ("coredll.dll")]
		protected static extern Wave.MMSYSERR waveOutRestart(IntPtr hwo);

		/// <summary>
		/// This structure contains timing information for different types of
		/// multimedia data.
		///		typedef struct mmtime_tag 
		///		{
		///			UINT wType; 
		///			union 
		///			{
		///				DWORD ms; 
		///				DWORD sample; 
		///				DWORD cb; 
		///				DWORD ticks; 
		///				struct 
		///				{
		///					BYTE hour; 
		///					BYTE min; 
		///					BYTE sec; 
		///					BYTE frame; 
		///					BYTE fps; 
		///					BYTE dummy; 
		///					BYTE pad[2]
		///				} smpte; 
		///				struct 
		///				{
		///					DWORD songptrpos;
		///				} midi; 
		///			} u; 
		///		} MMTIME;
		/// </summary>
		protected class MMTIME
		{
			/// <summary>
			/// Time format.
			/// </summary>
			public uint wType = 0;
			/// <summary>
			/// Byte count. Used when wType is TIME_BYTES.
			/// </summary>
			public uint cb = 0;

			// Padding because this is actually a union
			public uint pad = 0;
		} 

		// Used by MMTIME.wType
		protected const uint TIME_MS = 0x0001;
		protected const uint TIME_SAMPLES = 0x0002;
		protected const uint TIME_BYTES = 0x0004;
		protected const uint TIME_TICKS = 0x0020;

		/// <summary>
		/// This function retrieves the current playback position of the specified
		/// waveform output device.
		/// </summary>
		/// <param name="hwo">Handle to the waveform-audio output device.</param>
		/// <param name="pmmt">Pointer to an MMTIME structure.</param>
		/// <param name="cbmmt">Size, in bytes, of the MMTIME structure.</param>
		/// <returns>MMSYSERR</returns>
		[DllImport ("coredll.dll")]
		protected static extern Wave.MMSYSERR waveOutGetPosition(IntPtr hwo, MMTIME pmmt, uint cbmmt); 

		/// <summary>
		/// This structure describes the capabilities of a waveform-audio output device.
		///		typedef struct 
		///		{
		///			WORD wMid; 
		///			WORD wPid; 
		///			MMVERSION vDriverVersion; 
		///			TCHAR szPname[MAXPNAMELEN]; 
		///			DWORD dwFormats; 
		///			WORD wChannels; 
		///			WORD wReserved1; 
		///			DWORD dwSupport;} 
		///		WAVEOUTCAPS;
		///	This structure has an embedded TCHAR array so the managed implementation is
		///	a byte array with accessors.
		/// </summary>
		protected class WAVEOUTCAPS
		{
			const uint WAVEOUTCAPS_SIZE = 84;

			private byte[] m_data = null;
			public uint Size { get { return (uint)WAVEOUTCAPS_SIZE; } }

			/// <summary>
			/// Used by dwSupport in WAVEOUTCAPS
			/// Supports pitch control
			/// </summary>
			public const uint WAVECAPS_PITCH = 0x0001;
			/// <summary>
			/// Used by dwSupport in WAVEOUTCAPS
			/// Supports playback rate control
			/// </summary>
			public const uint WAVECAPS_PLAYBACKRATE = 0x0002;
			/// <summary>
			/// Used by dwSupport in WAVEOUTCAPS
			/// Supports volume control
			/// </summary>
			public const uint WAVECAPS_VOLUME = 0x0004;
			/// <summary>
			/// Used by dwSupport in WAVEOUTCAPS
			/// Supports separate left-right volume control
			/// </summary>
			public const uint WAVECAPS_LRVOLUME = 0x0008;

			/// <summary>
			/// Manufacturer identifier for the device driver for the device.
			/// Manufacturer identifiers are defined in Manufacturer and Product
			/// Identifiers.
			/// </summary>
			public ushort wMid { get { return BitConverter.ToUInt16(m_data, 0); } }
			/// <summary>
			/// Product identifier for the device. Product identifiers are defined in
			/// Manufacturer and Product Identifiers.
			/// </summary>
			public ushort wPid { get { return BitConverter.ToUInt16(m_data, 2); } }
			/// <summary>
			/// Version number of the device driver for the device. The high-order byte
			/// is the major version number, and the low-order byte is the minor version
			/// number.
			/// </summary>
			public uint vDriverVersion { get { return BitConverter.ToUInt32(m_data, 4); } }
			/// <summary>
			/// Specifies the standard formats that are supported.
			/// </summary>
			public uint dwFormats { get { return BitConverter.ToUInt32(m_data, 72); } }
			/// <summary>
			/// Number specifying whether the device supports mono (1) or stereo (2)
			/// output.
			/// </summary>
			public ushort wChannels { get { return BitConverter.ToUInt16(m_data, 76); } }
			/// <summary>
			/// Packing.
			/// </summary>
			public ushort wReserved1 { get { return BitConverter.ToUInt16(m_data, 78); } }
			/// <summary>
			/// Specifies the optional functionality supported by the device.
			/// </summary>
			public uint dwSupport { get { return BitConverter.ToUInt16(m_data, 80); } }
			/// <summary>
			/// Null-terminated string that contains the product name.
			/// </summary>
			public string szPname
			{
				get
				{
					char[] bytes = new char[32];
					for (int i = 0; i < 32; i++)
					{
						bytes[i] = (char)BitConverter.ToUInt16(m_data, i * 2 + 8);
					}

					return new string(bytes);
				}
			}

			public WAVEOUTCAPS()
			{
				m_data = new byte[WAVEOUTCAPS_SIZE];
			}

			public static implicit operator byte[](WAVEOUTCAPS caps)
			{
				return caps.m_data;
			}
		}
 
		/// <summary>
		/// This function queries a specified waveform device to determine its
		/// capabilities.
		/// </summary>
		/// <param name="uDeviceID">Identifier of the waveform-audio output device.
		/// It can be either a device identifier or a Handle to an open waveform-audio
		/// output device.</param>
		/// <param name="pwoc">Pointer to a WAVEOUTCAPS structure to be filled with
		/// information about the capabilities of the device.</param>
		/// <param name="cbwoc">Size, in bytes, of the WAVEOUTCAPS structure.</param>
		/// <returns>MMSYSERR</returns>
		[DllImport ("coredll.dll")]
		protected static extern Wave.MMSYSERR waveOutGetDevCaps(uint uDeviceID, byte[] pwoc, uint cbwoc);

		/// <summary>
		/// Run a test of the WaveOut class.
		/// </summary>
		/// <param name="showLine">Delegate called to show debug information</param>
		public static void SetVolume()
		{
			IntPtr hwo = IntPtr.Zero;
			Wave.WAVEFORMATEX wfmt = new Wave.WAVEFORMATEX();
			wfmt.nChannels = 2;
			
			Wave.MMSYSERR result = waveOutOpen(ref hwo, 0, wfmt, IntPtr.Zero, 0, Wave.CALLBACK_NULL);
			uint volume = 0;
			uint vol = ((uint)volume & 0x0000ffff) | ((uint)volume << 16);

			waveOutSetVolume(hwo, vol);
		}		
	}	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -