/// <summary> /// Creates a new WASAPI Output /// </summary> /// <param name="device">Device to use</param> /// <param name="shareMode"></param> /// <param name="useEventSync">true if sync is done with event. false use sleep.</param> /// <param name="latency"></param> public WasapiOut(MMDevice device, AudioClientShareMode shareMode, bool useEventSync, int latency) { this.audioClient = device.AudioClient; this.mmDevice = device; this.shareMode = shareMode; this.isUsingEventSync = useEventSync; this.latencyMilliseconds = latency; this.syncContext = SynchronizationContext.Current; }
/// <summary> /// Initializes a new instance of the <see cref="WasapiCapture"/> class. /// </summary> /// <param name="captureDevice">The capture device.</param> /// <param name="useEventSync">true if sync is done with event. false use sleep.</param> public WasapiCapture(MMDevice captureDevice, bool useEventSync) { syncContext = SynchronizationContext.Current; audioClient = captureDevice.AudioClient; ShareMode = AudioClientShareMode.Shared; isUsingEventSync = useEventSync; waveFormat = audioClient.MixFormat; }
private void CaptureThread(AudioClient client) { Exception exception = null; try { DoRecording(client); } catch (Exception e) { exception = e; } finally { client.Stop(); // don't dispose - the AudioClient only gets disposed when WasapiCapture is disposed } captureThread = null; RaiseRecordingStopped(exception); Debug.WriteLine("Stop wasapi"); }
/// <summary> /// Dispose /// </summary> public void Dispose() { StopRecording(); if (captureThread != null) { captureThread.Join(); captureThread = null; } if (audioClient != null) { audioClient.Dispose(); audioClient = null; } }
private void DoRecording(AudioClient client) { Debug.WriteLine(String.Format("Client buffer frame count: {0}", client.BufferSize)); int bufferFrameCount = client.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); int waitMilliseconds = (int)(3 * actualDuration / REFTIMES_PER_MILLISEC); AudioCaptureClient capture = client.AudioCaptureClient; client.Start(); if (isUsingEventSync) { Debug.WriteLine(string.Format("wait: {0} ms", waitMilliseconds)); } else { Debug.WriteLine(string.Format("sleep: {0} ms", sleepMilliseconds)); } while (!this.requestStop) { bool readBuffer = true; if (isUsingEventSync) { readBuffer = frameEventWaitHandle.WaitOne(waitMilliseconds, false); } else { Thread.Sleep(sleepMilliseconds); } // If still playing and notification is ok if (!this.requestStop && readBuffer) { ReadNextPacket(capture); } } }
/// <summary> /// Dispose /// </summary> public void Dispose() { if (audioClient != null) { Stop(); audioClient.Dispose(); audioClient = null; renderClient = null; } }