Exemplo n.º 1
0
 /// <summary>
 /// Stops capturing audio data.
 /// </summary>
 public void StopCapture()
 {
     if (this.wasapiCaptureClient != null)
     {
         this.wasapiCaptureClient.Dispose();
         this.wasapiCaptureClient = null;
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// Starts capturing audio data.
        /// </summary>
        /// <param name="targetLatencyInMs">
        /// The target maximum number of milliseconds of acceptable lag between
        /// live sound being produced and capture operation.
        /// </param>
        /// <param name="audioEngineBufferInMs">
        /// The amount of audio that may be buffered by the audio engine between
        /// reads.
        /// </param>
        /// <param name="gain">
        /// The gain to be applied to the captured audio.
        /// </param>
        /// <param name="outFormat">
        /// The desired output format of the captured audio.
        /// </param>
        /// <param name="speech">
        /// If true, optimizes the audio capture pipeline for speech recognition.
        /// </param>
        /// <param name="eventDrivenCapture">
        /// If true, initialize Windows audio capture in event-driven mode. The audio capture engine will call
        /// the <see cref="AudioDataAvailableCallback"/> handler as soon as data is available, at intervals
        /// determined by the audio engine (which may be less than the <paramref name="targetLatencyInMs"/>).
        /// This captures audio with the lowest possible latency while still allowing for buffering up to the
        /// amount of time specified by <paramref name="targetLatencyInMs"/> (when for example the system is
        /// under heavy load and the capture callback is unable to service audio packets at the rate at which
        /// the audio engine returns captured audio packets).
        /// </param>
        public void StartCapture(int targetLatencyInMs, int audioEngineBufferInMs, float gain, WaveFormat outFormat, bool speech, bool eventDrivenCapture)
        {
            if (this.wasapiCaptureClient != null)
            {
                this.StopCapture();
            }

            this.wasapiCaptureClient = new WasapiCaptureClient(this.audioDevice, eventDrivenCapture);

            // Create a callback delegate and marshal it to a function pointer. Keep a
            // reference to the delegate as a class field to prevent it from being GC'd.
            this.callbackDelegate = new AudioDataAvailableCallback(this.AudioDataAvailableCallback);

            // initialize the capture with the desired parameters
            this.wasapiCaptureClient.Initialize(targetLatencyInMs, audioEngineBufferInMs, gain, outFormat, this.callbackDelegate, speech);

            // tell WASAPI to start capturing
            this.wasapiCaptureClient.Start();
        }