📄 waveout.cs
字号:
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 TestProc(MainTest.DisplayLineDelegate showLine)
{
WaveOut wo = null;
try
{
wo = new WaveOut();
uint numDevices = wo.NumDevices();
if (numDevices < 1)
{
showLine("FAILURE: No valid sound drivers detected");
return;
}
showLine(string.Format("{0} device{1} detected:", numDevices, numDevices != 1 ? "s" : ""));
for (uint i = 0; i < numDevices; i++)
{
string prodName = "";
if (Wave.MMSYSERR.NOERROR != wo.GetDeviceName(i, ref prodName))
{
showLine(string.Format(" {0}: Failed to get name", i));
}
else
{
showLine(string.Format(" {0}: {1}", i, prodName));
}
}
String fileName = System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase;
fileName = Path.GetDirectoryName(fileName);
fileName = Path.Combine(fileName, "test.wav");
showLine("Playing sound test.wav");
showLine("Setting stream buffers to 512KB total");
if (Wave.MMSYSERR.NOERROR != wo.Play(fileName, 512*1024, 0xffff, 0xffff))
{
showLine("FAILURE: Failed to play sound test.wav");
}
uint soundLength = wo.Milliseconds();
int sleepTime = (int)((float)soundLength / 3.0f);
showLine(string.Format("Sleeping for {0} ms", sleepTime));
Thread.Sleep(sleepTime);
showLine("Pausing for 1 second...");
wo.Pause();
Thread.Sleep(1000);
showLine("Resuming...");
wo.Resume();
showLine("Waiting for sound to finish...");
sleepTime = 5;
int watchDog = 0;
int maxLoops = (int)(wo.Milliseconds() / sleepTime);
while(!wo.Done())
{
Thread.Sleep(sleepTime);
Application.DoEvents();
watchDog++;
if (watchDog > maxLoops)
{
showLine("FAILURE: Failed to detecte end of sound");
return;
}
}
showLine("Done playing sound");
}
finally
{
if (wo != null)
wo.Dispose();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -