using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; using NAudio.CoreAudioApi.Interfaces; namespace NAudio.CoreAudioApi { /// /// Audio Capture Client /// public class AudioCaptureClient : IDisposable { IAudioCaptureClient audioCaptureClientInterface; internal AudioCaptureClient(IAudioCaptureClient audioCaptureClientInterface) { this.audioCaptureClientInterface = audioCaptureClientInterface; } /// /// Gets a pointer to the buffer /// /// Pointer to the buffer public IntPtr GetBuffer( out int numFramesToRead, out AudioClientBufferFlags bufferFlags, out long devicePosition, out long qpcPosition) { IntPtr bufferPointer; Marshal.ThrowExceptionForHR(audioCaptureClientInterface.GetBuffer(out bufferPointer, out numFramesToRead, out bufferFlags, out devicePosition, out qpcPosition)); return bufferPointer; } /// /// Gets a pointer to the buffer /// /// Number of frames to read /// Buffer flags /// Pointer to the buffer public IntPtr GetBuffer( out int numFramesToRead, out AudioClientBufferFlags bufferFlags) { IntPtr bufferPointer; long devicePosition; long qpcPosition; Marshal.ThrowExceptionForHR(audioCaptureClientInterface.GetBuffer(out bufferPointer, out numFramesToRead, out bufferFlags, out devicePosition, out qpcPosition)); return bufferPointer; } /// /// Gets the size of the next packet /// public int GetNextPacketSize() { int numFramesInNextPacket; Marshal.ThrowExceptionForHR(audioCaptureClientInterface.GetNextPacketSize(out numFramesInNextPacket)); return numFramesInNextPacket; } /// /// Release buffer /// /// Number of frames written public void ReleaseBuffer(int numFramesWritten) { Marshal.ThrowExceptionForHR(audioCaptureClientInterface.ReleaseBuffer(numFramesWritten)); } #region IDisposable Members /// /// Release the COM object /// public void Dispose() { if (audioCaptureClientInterface != null) { // althugh GC would do this for us, we want it done now // to let us reopen WASAPI Marshal.ReleaseComObject(audioCaptureClientInterface); audioCaptureClientInterface = null; GC.SuppressFinalize(this); } } #endregion } }