📄 waveout.cs
字号:
/// </summary>
/// <returns>MMSYSERR.NOERROR if successful</returns>
public Wave.MMSYSERR Pause()
{
return waveOutPause(m_hwo);
}
/// <summary>
/// Get the volume of this sound.
/// </summary>
/// <param name="volLeft">Left channel volume level</param>
/// <param name="volRight">Right channel volume level</param>
/// <returns>MMSYSERR.NOERROR if successful</returns>
public Wave.MMSYSERR GetVolume(ref ushort volLeft, ref ushort volRight)
{
uint vol = 0;
Wave.MMSYSERR result = waveOutGetVolume(m_hwo, ref vol);
if (result != Wave.MMSYSERR.NOERROR)
return result;
volLeft = (ushort)(vol & 0x0000ffff);
volRight = (ushort)(vol >> 16);
return Wave.MMSYSERR.NOERROR;
}
/// <summary>
/// Sets the volume of this sound.
/// </summary>
/// <param name="volLeft">Left channel volume level</param>
/// <param name="volRight">Right channel volume level</param>
/// <returns>MMSYSERR.NOERROR if successful</returns>
public Wave.MMSYSERR SetVolume(ushort volLeft, ushort volRight)
{
uint vol = ((uint)volLeft & 0x0000ffff) | ((uint)volRight << 16);
return waveOutSetVolume(m_hwo, vol);
}
/// <summary>
/// Clean up all resources.
/// </summary>
public void Dispose()
{
Stop();
}
}
/// <summary>
/// Defines the MessageWindow used to receive messages from the audio
/// system.
/// </summary>
public class SoundMessageWindow : MessageWindow
{
public const int MM_WOM_OPEN = 0x03BB;
public const int MM_WOM_CLOSE = 0x03BC;
public const int MM_WOM_DONE = 0x03BD;
// Instance of a playback interface
protected WaveOut m_wo = null;
public SoundMessageWindow(WaveOut wo)
{
m_wo = wo;
}
protected override void WndProc(ref Message msg)
{
switch (msg.Msg)
{
// When this message is encountered, a block is
// done playing, so notify the WaveOut instance.
case MM_WOM_DONE:
m_wo.BlockDone(msg.WParam);
break;
}
base.WndProc(ref msg);
}
}
/// <summary>
/// Maintain an instance of a MessageWindow that handles audio messages.
/// </summary>
protected SoundMessageWindow m_msgWindow = null;
/// <summary>
/// An instance of WaveFile used as the source for playing audio.
/// </summary>
protected WaveFile m_file = null;
/// <summary>
/// Create an instance of WaveOut.
/// </summary>
public WaveOut()
{
m_msgWindow = new SoundMessageWindow(this);
m_file = new WaveFile();
}
/// <summary>
/// Determine the number of available playback devices.
/// </summary>
/// <returns>Number of output devices</returns>
public uint NumDevices()
{
return (uint)waveOutGetNumDevs();
}
/// <summary>
/// Get the name of the specified playback device.
/// </summary>
/// <param name="deviceId">ID of the device</param>
/// <param name="prodName">Destination string assigned the name</param>
/// <returns>MMSYSERR.NOERROR if successful</returns>
public Wave.MMSYSERR GetDeviceName(uint deviceId, ref string prodName)
{
WAVEOUTCAPS caps = new WAVEOUTCAPS();
Wave.MMSYSERR result = waveOutGetDevCaps(deviceId, caps, caps.Size);
if (result != Wave.MMSYSERR.NOERROR)
return result;
prodName = caps.szPname;
return Wave.MMSYSERR.NOERROR;
}
/// <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")]
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -