using System; 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) { Marshal.ThrowExceptionForHR(audioCaptureClientInterface.GetBuffer(out var 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) { Marshal.ThrowExceptionForHR(audioCaptureClientInterface.GetBuffer(out var bufferPointer, out numFramesToRead, out bufferFlags, out _, out _)); return bufferPointer; } /// /// Gets the size of the next packet /// public int GetNextPacketSize() { Marshal.ThrowExceptionForHR(audioCaptureClientInterface.GetNextPacketSize(out var numFramesInNextPacket)); return numFramesInNextPacket; } /// /// Release buffer /// /// Number of frames written public void ReleaseBuffer(int numFramesWritten) { Marshal.ThrowExceptionForHR(audioCaptureClientInterface.ReleaseBuffer(numFramesWritten)); } /// /// Release the COM object /// public void Dispose() { if (audioCaptureClientInterface != null) { // although GC would do this for us, we want it done now // to let us reopen WASAPI Marshal.ReleaseComObject(audioCaptureClientInterface); audioCaptureClientInterface = null; GC.SuppressFinalize(this); } } } }