private void ReadNextPacket(AudioCaptureClient capture) { IntPtr buffer; int framesAvailable; AudioClientBufferFlags flags; int packetSize = capture.GetNextPacketSize(); int recordBufferOffset = 0; //Debug.WriteLine(string.Format("packet size: {0} samples", packetSize / 4)); while (packetSize != 0) { buffer = capture.GetBuffer(out framesAvailable, out flags); int bytesAvailable = framesAvailable * bytesPerFrame; //Debug.WriteLine(string.Format("got buffer: {0} frames", framesAvailable)); // if not silence... if ((flags & AudioClientBufferFlags.Silent) != AudioClientBufferFlags.Silent) { Marshal.Copy(buffer, recordBuffer, recordBufferOffset, bytesAvailable); } else { Array.Clear(recordBuffer, recordBufferOffset, bytesAvailable); } recordBufferOffset += bytesAvailable; capture.ReleaseBuffer(framesAvailable); packetSize = capture.GetNextPacketSize(); } if (DataAvailable != null) { DataAvailable(this, new WaveInEventArgs(recordBuffer, recordBufferOffset)); } }
private void ReadNextPacket(AudioCaptureClient capture) { int packetSize = capture.GetNextPacketSize(); int recordBufferOffset = 0; //Debug.WriteLine(string.Format("packet size: {0} samples", packetSize / 4)); while (packetSize != 0) { int framesAvailable; AudioClientBufferFlags flags; IntPtr buffer = capture.GetBuffer(out framesAvailable, out flags); int bytesAvailable = framesAvailable * bytesPerFrame; // apparently it is sometimes possible to read more frames than we were expecting? // fix suggested by Michael Feld: int spaceRemaining = Math.Max(0, recordBuffer.Length - recordBufferOffset); if (spaceRemaining < bytesAvailable && recordBufferOffset > 0) { if (DataAvailable != null) { DataAvailable(this, new WaveInEventArgs(recordBuffer, recordBufferOffset)); } recordBufferOffset = 0; } if ((flags & AudioClientBufferFlags.DataDiscontinuity) == AudioClientBufferFlags.DataDiscontinuity) { Debug.WriteLine("DataDiscontinuity flagged!"); } // if not silence... if ((flags & AudioClientBufferFlags.Silent) != AudioClientBufferFlags.Silent) { Marshal.Copy(buffer, recordBuffer, recordBufferOffset, bytesAvailable); } else { Array.Clear(recordBuffer, recordBufferOffset, bytesAvailable); } recordBufferOffset += bytesAvailable; capture.ReleaseBuffer(framesAvailable); packetSize = capture.GetNextPacketSize(); } if (DataAvailable != null) { DataAvailable(this, new WaveInEventArgs(recordBuffer, recordBufferOffset)); } }
private void ReadNextPacket(AudioCaptureClient capture) { int nextPacketSize = capture.GetNextPacketSize(); int num = 0; while (nextPacketSize != 0) { int num2; AudioClientBufferFlags audioClientBufferFlags; IntPtr buffer = capture.GetBuffer(out num2, out audioClientBufferFlags); int num3 = num2 * this.bytesPerFrame; int num4 = Math.Max(0, this.recordBuffer.Length - num); if (num4 < num3 && num > 0) { if (this.DataAvailable != null) { this.DataAvailable(this, new WaveInEventArgs(this.recordBuffer, num)); } num = 0; } if ((audioClientBufferFlags & AudioClientBufferFlags.Silent) != AudioClientBufferFlags.Silent) { Marshal.Copy(buffer, this.recordBuffer, num, num3); } else { Array.Clear(this.recordBuffer, num, num3); } num += num3; capture.ReleaseBuffer(num2); nextPacketSize = capture.GetNextPacketSize(); } if (this.DataAvailable != null) { this.DataAvailable(this, new WaveInEventArgs(this.recordBuffer, num)); } }
private void ReadNextPacket(AudioCaptureClient capture) { IntPtr buffer; int framesAvailable; AudioClientBufferFlags flags; int packetSize = capture.GetNextPacketSize(); int recordBufferOffset = 0; //Debug.WriteLine(string.Format("packet size: {0} samples", packetSize / 4)); while (packetSize != 0) { buffer = capture.GetBuffer(out framesAvailable, out flags); int bytesAvailable = framesAvailable * bytesPerFrame; // apparently it is sometimes possible to read more frames than we were expecting? // fix suggested by Michael Feld: int spaceRemaining = Math.Max(0, recordBuffer.Length - recordBufferOffset); if (spaceRemaining < bytesAvailable && recordBufferOffset > 0) { if (DataAvailable != null) DataAvailable(this, new WaveInEventArgs(recordBuffer, recordBufferOffset)); recordBufferOffset = 0; } // if not silence... if ((flags & AudioClientBufferFlags.Silent) != AudioClientBufferFlags.Silent) { Marshal.Copy(buffer, recordBuffer, recordBufferOffset, bytesAvailable); } else { Array.Clear(recordBuffer, recordBufferOffset, bytesAvailable); } recordBufferOffset += bytesAvailable; capture.ReleaseBuffer(framesAvailable); packetSize = capture.GetNextPacketSize(); } if (DataAvailable != null) { DataAvailable(this, new WaveInEventArgs(recordBuffer, recordBufferOffset)); } }
private void ReadNextPacket(AudioCaptureClient capture) { IntPtr buffer; int framesAvailable; AudioClientBufferFlags flags; int packetSize = capture.GetNextPacketSize(); int recordBufferOffset = 0; //Debug.WriteLine(string.Format("packet size: {0} samples", packetSize / 4)); while (packetSize != 0) { buffer = capture.GetBuffer(out framesAvailable, out flags); int bytesAvailable = framesAvailable * bytesPerFrame; //Debug.WriteLine(string.Format("got buffer: {0} frames", framesAvailable)); // if not silence... if ((flags & AudioClientBufferFlags.Silent) != AudioClientBufferFlags.Silent) { Marshal.Copy(buffer, recordBuffer, recordBufferOffset, bytesAvailable); } else { Array.Clear(recordBuffer, recordBufferOffset, bytesAvailable); } recordBufferOffset += bytesAvailable; capture.ReleaseBuffer(framesAvailable); packetSize = capture.GetNextPacketSize(); } if (DataAvailable != null) { DataAvailable(this, new WaveInEventArgs(recordBuffer, recordBufferOffset)); } }
private void ReadNextPacket2(AudioCaptureClient capture1, AudioCaptureClient capture2) { int packetSize1 = capture1.GetNextPacketSize(); int packetSize2 = capture2.GetNextPacketSize(); int recordBufferOffset = 0; int recordBufferOffset2 = 0; while (packetSize1 != 0 || packetSize2 != 0) { int framesAvailable1; AudioClientBufferFlags flags1; int framesAvailable2; AudioClientBufferFlags flags2; IntPtr buffer1 = capture1.GetBuffer(out framesAvailable1, out flags1); IntPtr buffer2 = capture2.GetBuffer(out framesAvailable2, out flags2); int bytesAvailable1 = framesAvailable1 * bytesPerFrame; int bytesAvailable2 = framesAvailable2 * bytesPerFrame2; // apparently it is sometimes possible to read more frames than we were expecting? // fix suggested by Michael Feld: int spaceRemaining = Math.Max(0, recordBuffer.Length - recordBufferOffset); int spaceRemaining2 = Math.Max(0, recordBuffer2.Length - recordBufferOffset2); if ((spaceRemaining < bytesAvailable1 && recordBufferOffset > 0) || (spaceRemaining2 < bytesAvailable2 && recordBufferOffset2 > 0)) { int outputIndex = 0; int maxLen = Math.Max(recordBufferOffset, recordBufferOffset2); var destWaveBuffer = new WaveBuffer(new byte[maxLen]); for (int i = 0; i < maxLen; i += 4) { float a, b; if (i < recordBuffer.Length) { a = BitConverter.ToSingle(recordBuffer, i); } else { a = 0; } if (i < recordBuffer2.Length) { b = BitConverter.ToSingle(recordBuffer2, i); } else { b = 0; } float c = a + b; if (c >= 1.0f) { c = 1.0f; } else if (c <= -1.0f) { c = -1.0f; } destWaveBuffer.ShortBuffer[outputIndex++] = (short)(c * 32767); } if (DataAvailable != null) { DataAvailable(this, new WaveInEventArgs(destWaveBuffer.ByteBuffer, maxLen / 2)); } recordBufferOffset = 0; recordBufferOffset2 = 0; } // if not silence... if (packetSize1 != 0 && (flags1 & AudioClientBufferFlags.Silent) != AudioClientBufferFlags.Silent) { Marshal.Copy(buffer1, recordBuffer, recordBufferOffset, bytesAvailable1); } if (packetSize2 != 0 && (flags2 & AudioClientBufferFlags.Silent) != AudioClientBufferFlags.Silent) { Marshal.Copy(buffer2, recordBuffer2, recordBufferOffset2, bytesAvailable2); } if ((flags1 & AudioClientBufferFlags.Silent) == AudioClientBufferFlags.Silent) { Array.Clear(recordBuffer, recordBufferOffset, bytesAvailable1); } if ((flags2 & AudioClientBufferFlags.Silent) == AudioClientBufferFlags.Silent) { Array.Clear(recordBuffer2, recordBufferOffset2, bytesAvailable2); } recordBufferOffset += bytesAvailable1; recordBufferOffset2 += bytesAvailable2; capture1.ReleaseBuffer(framesAvailable1); capture2.ReleaseBuffer(framesAvailable2); packetSize1 = capture1.GetNextPacketSize(); packetSize2 = capture2.GetNextPacketSize(); } if (DataAvailable != null) { int outputIndex = 0; int maxLen = Math.Max(recordBufferOffset, recordBufferOffset2); var destWaveBuffer = new WaveBuffer(new byte[maxLen / 2]); for (int i = 0; i < maxLen; i += 4) { float a, b; if (i < recordBuffer.Length) { a = BitConverter.ToSingle(recordBuffer, i); } else { a = 0; } if (i < recordBuffer2.Length) { b = BitConverter.ToSingle(recordBuffer2, i); } else { b = 0; } float c = a + b; if (c >= 1.0f) { c = 1.0f; } else if (c <= -1.0f) { c = -1.0f; } destWaveBuffer.ShortBuffer[outputIndex++] = (short)(c * 32767); } DataAvailable(this, new WaveInEventArgs(destWaveBuffer.ByteBuffer, maxLen / 2)); } }