/* * Stop the audio capture, if currently recording. Properly disposes member objects. */ private void StopCapture() { if (capture.RecordingState == RecordingState.Recording) { capture.Stop(); finalSource.Dispose(); notificationSource.Dispose(); capture.Dispose(); sampleHandler = null; } }
/* * Begin audio capture. Connects to WASAPI, initializes the sample handler, and begins * sending captured data to it. */ private void StartCapture() { // Initialize hardware capture capture = new WasapiLoopbackCapture(); capture.Initialize(); // Init sample handler sampleHandler = new SampleHandler(capture.WaveFormat.Channels, capture.WaveFormat.SampleRate); // Configure per-block reads rather than per-sample reads notificationSource = new SingleBlockNotificationStream(new SoundInSource(capture).ToSampleSource()); notificationSource.SingleBlockRead += (s, e) => sampleHandler.Add(e.Left, e.Right); finalSource = notificationSource.ToWaveSource(); capture.DataAvailable += (s, e) => finalSource.Read(e.Data, e.Offset, e.ByteCount); // Start capture capture.Start(); }