public static WaveOutputPort CreateOutputPort(int deviceID, int channels, int sampleRate, int bitsPerSample)
{
int bytesPerSample = bitsPerSample / 8;
WAVEFORMATEX wfx = WAVEFORMATEX.CreatePCMFormat(channels, sampleRate, bitsPerSample);
// Query to see if the specified format is supported
IntPtr tmpHandle = new IntPtr();
MMSYSERROR result = winmm.waveOutOpen(ref tmpHandle, deviceID, wfx, null, IntPtr.Zero, winmm.WAVE_FORMAT_QUERY);
// If there was any error, return null
if (winmm.MMSYSERR_NOERROR != result)
return null;
WaveOutputPort outPort = new WaveOutputPort(deviceID, wfx);
return outPort;
}