public int Close()
 {
     int mmr = WaveConstants.MMSYSERR_INVALHANDLE;
     if (IsOpen())
     {
         mmr = Stop();
         mmr = WaveOutput.waveOutClose(_hWaveOut);
         if (mmr == WaveConstants.MMSYSERR_NOERROR)
         {
             Interlocked.Exchange(ref _DeviceState, (int)WaveStatus.waveClosed);
             _hWaveOut = IntPtr.Zero;
             _Callback = null;
         }
     }
     return mmr;
 }
 public int Open(int deviceId, WaveFormat wfmt)
 {
     int mmr = WaveConstants.MMSYSERR_HANDLEBUSY;
     if (!IsOpen())
     {
         _Callback = new DriverCallback(waveOutProc);
         GCHandle handle = GCHandle.Alloc(_Callback);
         mmr = WaveOutput.waveOutOpen(ref _hWaveOut, deviceId, ref wfmt.wfmt.Format,
                                   Marshal.GetFunctionPointerForDelegate(_Callback),
                                   IntPtr.Zero,
                                   WaveConstants.CALLBACK_FUNCTION);
         handle.Free();
         if (mmr == WaveConstants.MMSYSERR_NOERROR)
         {
             Interlocked.Exchange(ref _DeviceState, (int)WaveStatus.waveStopped);
         }
     }
     return mmr;
 }
        private IWaveNotifyHandler _WaveNotify; // Audio Event handler

        #endregion Fields

        #region Constructors

        public WaveOutDevice()
        {
            _hWaveOut = IntPtr.Zero;
            _DeviceState = (int)WaveStatus.waveClosed;
            _AudioBuffers = new Dictionary<int, WaveBuffer>();
            _WaveNotify = null;
            _Callback = null;
        }