/*
         * Stop the audio capture, if currently recording. Properly disposes member objects.
         */
        private void StopCapture()
        {
            if (wasapiCapture.RecordingState == RecordingState.Recording)
            {
                wasapiCapture.Stop();

                finalSource.Dispose();
                notificationSource.Dispose();
                wasapiCapture.Dispose();

                SampleHandler = null;
            }
        }
Esempio n. 2
0
        /*
         * Initializes WASAPI, initializes the sample handler, and sends captured data to it.
         */
        private void StartCapture()
        {
            // Initialize hardware capture
            wasapiCapture = new WasapiLoopbackCapture(25);
            wasapiCapture.Initialize();

            // Initialize sample handler
            SampleHandler = new SampleHandler(wasapiCapture.WaveFormat.Channels);

            // Configure per-block reads rather than per-sample reads
            notificationSource = new SingleBlockNotificationStream(new SoundInSource(wasapiCapture).ToSampleSource());
            notificationSource.SingleBlockRead += (s, e) => SampleHandler.Add(e.Left, e.Right);
            finalSource = notificationSource.ToWaveSource();
            wasapiCapture.DataAvailable += (s, e) => finalSource.Read(e.Data, e.Offset, e.ByteCount);

            // Start capture
            wasapiCapture.Start();
        }