/// <summary>Initialises the WaveOut device</summary> /// <param name="waveProvider">WaveProvider to play</param> private void Init(IWaveProvider waveProvider) { if (this._playbackState != PlaybackState.Stopped) { throw new InvalidOperationException("Can't re-initialize during playback"); } if (this._hWaveOut != IntPtr.Zero) { this.DisposeBuffers(); this.CloseWaveOut(); } this._callbackEvent = new AutoResetEvent(false); this._waveStream = waveProvider; int byteSize = waveProvider.WaveFormat.ConvertLatencyToByteSize((this.DesiredLatency + this.NumberOfBuffers - 1) / this.NumberOfBuffers); MmResult result; lock (this._waveOutLock) result = AudioInterop.waveOutOpenWindow(out this._hWaveOut, (IntPtr)this.DeviceNumber, this._waveStream.WaveFormat, this._callbackEvent.SafeWaitHandle.DangerousGetHandle(), IntPtr.Zero, AudioInterop.WaveInOutOpenFlags.CallbackEvent); MmException.Try(result, "waveOutOpen"); this._buffers = new AudioBuffer[this.NumberOfBuffers]; this._playbackState = PlaybackState.Stopped; for (int index = 0; index < this.NumberOfBuffers; ++index) { this._buffers[index] = new AudioBuffer(this._hWaveOut, byteSize, this._waveStream, this._waveOutLock); } }
/// <summary>Releases resources held by this WaveBuffer</summary> protected void Dispose(bool disposing) { int num1 = disposing ? 1 : 0; if (_hHeader.IsAllocated) { _hHeader.Free(); } if (_hBuffer.IsAllocated) { _hBuffer.Free(); } if (_hThis.IsAllocated) { _hThis.Free(); } if (!(_hWaveOut != IntPtr.Zero)) { return; } lock (_waveOutLock) { int num2 = (int)AudioInterop.waveOutUnprepareHeader(_hWaveOut, _header, Marshal.SizeOf((object)_header)); } _hWaveOut = IntPtr.Zero; }
public static float GetWaveOutVolume(IntPtr hWaveOut, object lockObject) { int dwVolume; MmResult volume; lock (lockObject) volume = AudioInterop.waveOutGetVolume(hWaveOut, out dwVolume); MmException.Try(volume, "waveOutGetVolume"); return((dwVolume & ushort.MaxValue) / (float)ushort.MaxValue); }
private void WriteToWaveOut() { MmResult result; lock (_waveOutLock) result = AudioInterop.waveOutWrite(_hWaveOut, _header, Marshal.SizeOf((object)_header)); if (result != MmResult.NoError) { throw new MmException(result, "waveOutWrite"); } GC.KeepAlive((object)this); }
public static long GetPositionBytes(IntPtr hWaveOut, object lockObject) { lock (lockObject) { MmTime mmTime = new MmTime(); mmTime.wType = 4U; MmException.Try(AudioInterop.waveOutGetPosition(hWaveOut, ref mmTime, Marshal.SizeOf((object)mmTime)), "waveOutGetPosition"); if (mmTime.wType != 4U) { throw new Exception($"waveOutGetPosition: wType -> Expected {4}, Received {mmTime.wType}"); } return(mmTime.cb); } }
/// <summary>Resume playing after a pause from the same position</summary> private void Resume() { if (this._playbackState != PlaybackState.Paused) { return; } MmResult result; lock (this._waveOutLock) result = AudioInterop.waveOutRestart(this._hWaveOut); if (result != MmResult.NoError) { throw new MmException(result, "waveOutRestart"); } this._playbackState = PlaybackState.Playing; }
private void CloseWaveOut() { if (this._callbackEvent != null) { this._callbackEvent.Close(); this._callbackEvent = (AutoResetEvent)null; } lock (this._waveOutLock) { if (!(this._hWaveOut != IntPtr.Zero)) { return; } int num = (int)AudioInterop.waveOutClose(this._hWaveOut); this._hWaveOut = IntPtr.Zero; } }
/// <summary>Stop and reset the WaveOut device</summary> public void Stop() { if (this._playbackState == PlaybackState.Stopped) { return; } this._playbackState = PlaybackState.Stopped; MmResult result; lock (this._waveOutLock) result = AudioInterop.waveOutReset(this._hWaveOut); if (result != MmResult.NoError) { throw new MmException(result, "waveOutReset"); } this._callbackEvent.Set(); }
/// <summary>Pause the audio</summary> public void Pause() { if (this._playbackState != PlaybackState.Playing) { return; } this._playbackState = PlaybackState.Paused; Music.IconKind = PackIconKind.PlayCircleOutline; MmResult result; lock (this._waveOutLock) result = AudioInterop.waveOutPause(this._hWaveOut); if (result != MmResult.NoError) { throw new MmException(result, "waveOutPause"); } }
public static void SetWaveOutVolume(float value, IntPtr hWaveOut, object lockObject) { if (value < 0.0) { throw new ArgumentOutOfRangeException(nameof(value), "Volume must be between 0.0 and 1.0"); } if (value > 1.0) { throw new ArgumentOutOfRangeException(nameof(value), "Volume must be between 0.0 and 1.0"); } int dwVolume = (int)(value * (double)ushort.MaxValue) + ((int)(value * (double)ushort.MaxValue) << 16); MmResult result; lock (lockObject) result = AudioInterop.waveOutSetVolume(hWaveOut, dwVolume); MmException.Try(result, "waveOutSetVolume"); }
/// <summary>creates a new wavebuffer</summary> /// <param name="hWaveOut">WaveOut device to write to</param> /// <param name="bufferSize">Buffer size in bytes</param> /// <param name="bufferFillStream">Stream to provide more data</param> /// <param name="waveOutLock">Lock to protect WaveOut API's from being called on >1 thread</param> public AudioBuffer( IntPtr hWaveOut, int bufferSize, IWaveProvider bufferFillStream, object waveOutLock) { BufferSize = bufferSize; _buffer = new byte[bufferSize]; _hBuffer = GCHandle.Alloc((object)_buffer, GCHandleType.Pinned); _hWaveOut = hWaveOut; _waveStream = bufferFillStream; _waveOutLock = waveOutLock; _header = new WaveHeader(); _hHeader = GCHandle.Alloc((object)_header, GCHandleType.Pinned); _header.dataBuffer = _hBuffer.AddrOfPinnedObject(); _header.bufferLength = bufferSize; _header.loops = 1; _hThis = GCHandle.Alloc((object)this); _header.userData = (IntPtr)_hThis; lock (waveOutLock) MmException.Try(AudioInterop.waveOutPrepareHeader(hWaveOut, _header, Marshal.SizeOf((object)_header)), "waveOutPrepareHeader"); }