private void ReadData(byte[] buffer, AudioCaptureClient captureClient, uint frameSize) { int nextPacketSize = captureClient.GetNextPacketSize(); int read = 0; int offset = 0; while (nextPacketSize != 0) { int framesAvailable; AudioClientBufferFlags flags; IntPtr nativeBuffer = captureClient.GetBuffer(out framesAvailable, out flags); int bytesAvailable = (int)(framesAvailable * frameSize); int bytesToCopy = Math.Min(bytesAvailable, buffer.Length); if (Math.Max(buffer.Length - read, 0) < bytesAvailable && read > 0) { RaiseDataAvailable(buffer, 0, read); read = offset = 0; } if ((flags & AudioClientBufferFlags.Silent) == AudioClientBufferFlags.Silent) { Array.Clear(buffer, offset, bytesToCopy); } else { Marshal.Copy(nativeBuffer, buffer, offset, bytesToCopy); } read += bytesToCopy; offset += bytesToCopy; captureClient.ReleaseBuffer(framesAvailable); nextPacketSize = captureClient.GetNextPacketSize(); } RaiseDataAvailable(buffer, 0, read); }