/// <summary> /// Begin to play /// </summary> internal override void End() { if (!_deviceOpen) { System.Diagnostics.Debug.Assert(false); throw new InvalidOperationException(); } lock (_noWriteOutLock) { _deviceOpen = false; CheckForAbort(); if (_queueIn.Count != 0) { Interop.WinMM.waveOutReset(_hwo); } // Close it; no point in returning errors if this fails Interop.WinMM.MMSYSERR result = Interop.WinMM.waveOutClose(_hwo); if (result != Interop.WinMM.MMSYSERR.NOERROR) { // This may create a dead lock System.Diagnostics.Debug.Assert(false); } } }
/// <summary> /// Play a wave file. /// </summary> internal override void Play(byte[] buffer) { if (!_deviceOpen) { System.Diagnostics.Debug.Assert(false); } else { int bufferSize = buffer.Length; _bytesWritten += bufferSize; System.Diagnostics.Debug.Assert(bufferSize % _blockAlign == 0); WaveHeader waveHeader = new(buffer); GCHandle waveHdr = waveHeader.WAVEHDR; Interop.WinMM.MMSYSERR result = Interop.WinMM.waveOutPrepareHeader(_hwo, waveHdr.AddrOfPinnedObject(), waveHeader.SizeHDR); if (result != Interop.WinMM.MMSYSERR.NOERROR) { throw new AudioException(result); } lock (_noWriteOutLock) { if (!_aborted) { lock (_queueIn) { InItem item = new(waveHeader); _queueIn.Add(item); // Something in the queue cannot exit anymore _evt.Reset(); } // Start playback of the first buffer result = Interop.WinMM.waveOutWrite(_hwo, waveHdr.AddrOfPinnedObject(), waveHeader.SizeHDR); if (result != Interop.WinMM.MMSYSERR.NOERROR) { lock (_queueIn) { _queueIn.RemoveAt(_queueIn.Count - 1); throw new AudioException(result); } } } } } }
/// <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> internal static Interop.WinMM.MMSYSERR GetDeviceName(int deviceId, [MarshalAs(UnmanagedType.LPWStr)] out string prodName) { prodName = string.Empty; Interop.WinMM.WAVEOUTCAPS caps = new(); Interop.WinMM.MMSYSERR result = Interop.WinMM.waveOutGetDevCaps((IntPtr)deviceId, ref caps, Marshal.SizeOf <Interop.WinMM.WAVEOUTCAPS>()); if (result != Interop.WinMM.MMSYSERR.NOERROR) { return(result); } prodName = caps.szPname; return(Interop.WinMM.MMSYSERR.NOERROR); }
private void ClearBuffers() { foreach (InItem item in _queueOut) { WaveHeader waveHeader = item._waveHeader; Interop.WinMM.MMSYSERR result = Interop.WinMM.waveOutUnprepareHeader( _hwo, waveHeader.WAVEHDR.AddrOfPinnedObject(), waveHeader.SizeHDR); if (result != Interop.WinMM.MMSYSERR.NOERROR) { //System.Diagnostics.Debug.Assert (false); } waveHeader.Dispose(); } _queueOut.Clear(); }
/// <summary> /// Resume the playback of a paused sound. /// </summary> internal override void Resume() { lock (_noWriteOutLock) { if (!_aborted && _fPaused) { if (_deviceOpen) { Interop.WinMM.MMSYSERR result = Interop.WinMM.waveOutRestart(_hwo); if (result != Interop.WinMM.MMSYSERR.NOERROR) { System.Diagnostics.Debug.Assert(false); } } } } _fPaused = false; }
/// <summary> /// Pause the playback of a sound. /// </summary> internal override void Pause() { lock (_noWriteOutLock) { if (!_aborted && !_fPaused) { if (_deviceOpen) { Interop.WinMM.MMSYSERR result = Interop.WinMM.waveOutPause(_hwo); if (result != Interop.WinMM.MMSYSERR.NOERROR) { System.Diagnostics.Debug.Assert(false, ((int)result).ToString(System.Globalization.CultureInfo.InvariantCulture)); } } _fPaused = true; } } }
internal AudioException(Interop.WinMM.MMSYSERR errorCode) : base(string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0} - Error Code: 0x{1:x}", SR.Get(SRID.AudioDeviceError), (int)errorCode)) { }