/// <summary> /// Dispose /// </summary> public void Dispose() { StopRecording(); if (audioClient != null) { audioClient.Dispose(); audioClient = null; } }
/// <summary> /// Dispose /// </summary> public void Dispose() { StopRecording(); if (captureThread != null) { captureThread.Join(); captureThread = null; } if (audioClient != null) { audioClient.Dispose(); audioClient = null; } }
private void CaptureThread(AudioClient client) { Debug.WriteLine(client.BufferSize); int bufferFrameCount = audioClient.BufferSize; // Calculate the actual duration of the allocated buffer. long actualDuration = (long)((double)REFTIMES_PER_SEC * bufferFrameCount / WaveFormat.SampleRate); int sleepMilliseconds = (int)(actualDuration / REFTIMES_PER_MILLISEC / 2); AudioCaptureClient capture = client.AudioCaptureClient; client.Start(); try { Debug.WriteLine(string.Format("sleep: {0} ms", sleepMilliseconds)); while (!this.stop) { Thread.Sleep(sleepMilliseconds); ReadNextPacket(capture); } client.Stop(); if (RecordingStopped != null) { RecordingStopped(this, EventArgs.Empty); } } finally { if (capture != null) { capture.Dispose(); } if (client != null) { client.Dispose(); } client = null; capture = null; } System.Diagnostics.Debug.WriteLine("stop wasapi"); }
private void CaptureThread(AudioClient client) { Debug.WriteLine(client.BufferSize); int bufferFrameCount = audioClient.BufferSize; // Calculate the actual duration of the allocated buffer. long actualDuration = (long) ((double)REFTIMES_PER_SEC * bufferFrameCount / WaveFormat.SampleRate); int sleepMilliseconds = (int)(actualDuration / REFTIMES_PER_MILLISEC / 2); AudioCaptureClient capture = client.AudioCaptureClient; client.Start(); try { Debug.WriteLine(string.Format("sleep: {0} ms", sleepMilliseconds)); while (!this.stop) { Thread.Sleep(sleepMilliseconds); ReadNextPacket(capture); } client.Stop(); if (RecordingStopped != null) { RecordingStopped(this, null); } } finally { if (capture != null) { capture.Dispose(); } if (client != null) { client.Dispose(); } client = null; capture = null; } System.Diagnostics.Debug.WriteLine("stop wasapi"); }
private void DoRecording(AudioClient client) { Debug.WriteLine(client.BufferSize); var buf = new Byte[client.BufferSize * bytesPerFrame]; int bufLength = 0; int minPacketSize = waveFormat.AverageBytesPerSecond / 100; //100ms IntPtr hEvent = NativeMethods.CreateEventEx(IntPtr.Zero, IntPtr.Zero, 0, EventAccess.EVENT_ALL_ACCESS); client.SetEventHandle(hEvent); try { AudioCaptureClient capture = client.AudioCaptureClient; client.Start(); int packetSize = capture.GetNextPacketSize(); while (!this.stop) { IntPtr pData = IntPtr.Zero; int numFramesToRead = 0; AudioClientBufferFlags dwFlags = 0; if (packetSize == 0) { if (NativeMethods.WaitForSingleObjectEx(hEvent, 100, true) != 0) { throw new Exception("Capture event timeout"); } } pData = capture.GetBuffer(out numFramesToRead, out dwFlags); if ((int)(dwFlags & AudioClientBufferFlags.Silent) > 0) { pData = IntPtr.Zero; } if (numFramesToRead == 0) { continue; } int capturedBytes = numFramesToRead * bytesPerFrame; if (pData == IntPtr.Zero) { Array.Clear(buf, bufLength, capturedBytes); } else { Marshal.Copy(pData, buf, bufLength, capturedBytes); } bufLength += capturedBytes; capture.ReleaseBuffer(numFramesToRead); if (bufLength >= minPacketSize) { if (DataAvailable != null) { DataAvailable(this, new WaveInEventArgs(buf, bufLength)); } bufLength = 0; } packetSize = capture.GetNextPacketSize(); } } catch (Exception ex) { RaiseRecordingStopped(ex); Debug.WriteLine("stop wasapi"); } finally { RaiseRecordingStopped(null); NativeMethods.CloseHandle(hEvent); client.Stop(); client.Dispose(); } }
private void CaptureThread(AudioClient client) { Exception exception = null; try { DoRecording(client); } catch (Exception e) { exception = e; } finally { client.Stop(); client.Dispose(); } RaiseRecordingStopped(exception); Debug.WriteLine("stop wasapi"); }