/// <summary>Initialises the WaveOut device</summary> /// <param name="waveProvider">WaveProvider to play</param> public 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 = MyInterop.waveOutOpenWindow(out this.hWaveOut, (IntPtr)this.DeviceNumber, this.waveStream.WaveFormat, this.callbackEvent.SafeWaitHandle.DangerousGetHandle(), IntPtr.Zero, MyInterop.WaveInOutOpenFlags.CallbackEvent); MmException.Try(result, "waveOutOpen"); this.buffers = new MyBuffer[this.NumberOfBuffers]; this.playbackState = PlaybackState.Stopped; for (int index = 0; index < this.NumberOfBuffers; ++index) { this.buffers[index] = new MyBuffer(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 (this.hHeader.IsAllocated) { this.hHeader.Free(); } if (this.hBuffer.IsAllocated) { this.hBuffer.Free(); } if (this.hThis.IsAllocated) { this.hThis.Free(); } if (!(this.hWaveOut != IntPtr.Zero)) { return; } lock (this.waveOutLock) { int num2 = (int)MyInterop.waveOutUnprepareHeader(this.hWaveOut, this.header, Marshal.SizeOf((object)this.header)); } this.hWaveOut = IntPtr.Zero; }
public static float GetWaveOutVolume(IntPtr hWaveOut, object lockObject) { int dwVolume; MmResult volume; lock (lockObject) volume = MyInterop.waveOutGetVolume(hWaveOut, out dwVolume); MmException.Try(volume, "waveOutGetVolume"); return((float)(dwVolume & (int)ushort.MaxValue) / (float)ushort.MaxValue); }
private void WriteToWaveOut() { MmResult result; lock (this.waveOutLock) result = MyInterop.waveOutWrite(this.hWaveOut, this.header, Marshal.SizeOf((object)this.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(MyInterop.waveOutGetPosition(hWaveOut, ref mmTime, Marshal.SizeOf((object)mmTime)), "waveOutGetPosition"); if (mmTime.wType != 4U) { throw new Exception(string.Format("waveOutGetPosition: wType -> Expected {0}, Received {1}", (object)4, (object)mmTime.wType)); } return((long)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 = MyInterop.waveOutRestart(this.hWaveOut); if (result != MmResult.NoError) { throw new MmException(result, "waveOutRestart"); } this.playbackState = PlaybackState.Playing; }
/// <summary>Pause the audio</summary> public void Pause() { if (this.playbackState != PlaybackState.Playing) { return; } this.playbackState = PlaybackState.Paused; MmResult result; lock (this.waveOutLock) result = MyInterop.waveOutPause(this.hWaveOut); if (result != MmResult.NoError) { throw new MmException(result, "waveOutPause"); } }
public static void SetWaveOutVolume(float value, IntPtr hWaveOut, object lockObject) { if ((double)value < 0.0) { throw new ArgumentOutOfRangeException(nameof(value), "Volume must be between 0.0 and 1.0"); } if ((double)value > 1.0) { throw new ArgumentOutOfRangeException(nameof(value), "Volume must be between 0.0 and 1.0"); } int dwVolume = (int)((double)value * (double)ushort.MaxValue) + ((int)((double)value * (double)ushort.MaxValue) << 16); MmResult result; lock (lockObject) result = MyInterop.waveOutSetVolume(hWaveOut, dwVolume); MmException.Try(result, "waveOutSetVolume"); }
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)MyInterop.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 = MyInterop.waveOutReset(this.hWaveOut); if (result != MmResult.NoError) { throw new MmException(result, "waveOutReset"); } this.callbackEvent.Set(); }
/// <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 MyBuffer( IntPtr hWaveOut, int bufferSize, IWaveProvider bufferFillStream, object waveOutLock) { this.bufferSize = bufferSize; this.buffer = new byte[bufferSize]; this.hBuffer = GCHandle.Alloc((object)this.buffer, GCHandleType.Pinned); this.hWaveOut = hWaveOut; this.waveStream = bufferFillStream; this.waveOutLock = waveOutLock; this.header = new WaveHeader(); this.hHeader = GCHandle.Alloc((object)this.header, GCHandleType.Pinned); this.header.dataBuffer = this.hBuffer.AddrOfPinnedObject(); this.header.bufferLength = bufferSize; this.header.loops = 1; this.hThis = GCHandle.Alloc((object)this); this.header.userData = (IntPtr)this.hThis; lock (waveOutLock) MmException.Try(MyInterop.waveOutPrepareHeader(hWaveOut, this.header, Marshal.SizeOf((object)this.header)), "waveOutPrepareHeader"); }