Share via


SpeechRecognizer Class

Definition

Transcribes speech into text. Speech can arrive via microphone, audio file, or other audio input stream.

public sealed class SpeechRecognizer : Microsoft.CognitiveServices.Speech.Recognizer
type SpeechRecognizer = class
    inherit Recognizer
Public NotInheritable Class SpeechRecognizer
Inherits Recognizer
Inheritance
SpeechRecognizer

Examples

This example uses the speech recognizer from a microphone and listens to events generated by the recognizer.

public async Task SpeechContinuousRecognitionAsync()
{
    // Creates an instance of a speech config with specified subscription key and region.
    // Replace with your own subscription key and service region (e.g., "westus").
    var config = SpeechConfig.FromSubscription("YourSubscriptionKey", "YourServiceRegion");

    // Creates a speech recognizer from microphone.
    using (var recognizer = new SpeechRecognizer(config))
    {
        // Subscribes to events.
        recognizer.Recognizing += (s, e) => {
            Console.WriteLine($"RECOGNIZING: Text={e.Result.Text}");
        };

        recognizer.Recognized += (s, e) => {
            var result = e.Result;
            Console.WriteLine($"Reason: {result.Reason.ToString()}");
            if (result.Reason == ResultReason.RecognizedSpeech)
            {
                    Console.WriteLine($"Final result: Text: {result.Text}.");
            }
        };

        recognizer.Canceled += (s, e) => {
            Console.WriteLine($"\n    Canceled. Reason: {e.Reason.ToString()}, CanceledReason: {e.Reason}");
        };

        recognizer.SessionStarted += (s, e) => {
            Console.WriteLine("\n    Session started event.");
        };

        recognizer.SessionStopped += (s, e) => {
            Console.WriteLine("\n    Session stopped event.");
        };

        // Starts continuous recognition. 
        // Uses StopContinuousRecognitionAsync() to stop recognition.
        await recognizer.StartContinuousRecognitionAsync().ConfigureAwait(false);

        do
        {
            Console.WriteLine("Press Enter to stop");
        } while (Console.ReadKey().Key != ConsoleKey.Enter);

        // Stops recognition.
        await recognizer.StopContinuousRecognitionAsync().ConfigureAwait(false);
    }
}

Remarks

See also: Get started with speech-to-text

Constructors

SpeechRecognizer(EmbeddedSpeechConfig, AudioConfig)

Creates a new instance of SpeechRecognizer using EmbeddedSpeechConfig, configured to receive speech from an audio source specified in an AudioConfig object. Added in 1.19.0

SpeechRecognizer(EmbeddedSpeechConfig, AutoDetectSourceLanguageConfig, AudioConfig)

Creates a new instance of SpeechRecognizer, using EmbeddedSpeechConfig, that determines the source language from a list of options. Added in 1.20.0

SpeechRecognizer(EmbeddedSpeechConfig, AutoDetectSourceLanguageConfig)

Creates a new instance of SpeechRecognizer, using EmbeddedSpeechConfig, that determines the source language from a list of options. Added in 1.20.0

SpeechRecognizer(EmbeddedSpeechConfig)

Creates a new instance of SpeechRecognizer using EmbeddedSpeechConfig, configured to receive speech from the default microphone. Added in 1.19.0

SpeechRecognizer(HybridSpeechConfig, AudioConfig)

Creates a new instance of SpeechRecognizer using HybridSpeechConfig, configured to receive speech from an audio source specified in an AudioConfig object.

SpeechRecognizer(HybridSpeechConfig, AutoDetectSourceLanguageConfig, AudioConfig)

Creates a new instance of SpeechRecognizer, using HybridSpeechConfig, that determines the source language from a list of options.

SpeechRecognizer(HybridSpeechConfig, AutoDetectSourceLanguageConfig)

Creates a new instance of SpeechRecognizer, using HybridSpeechConfig, that determines the source language from a list of options.

SpeechRecognizer(HybridSpeechConfig)

Creates a new instance of SpeechRecognizer using HybridSpeechConfig, configured to receive speech from the default microphone.

SpeechRecognizer(SpeechConfig, AudioConfig)

Creates a new instance of SpeechRecognizer configured to receive speech from an audio source specified in an AudioConfig object.

SpeechRecognizer(SpeechConfig, AutoDetectSourceLanguageConfig, AudioConfig)

Creates a new instance of SpeechRecognizer. Added in 1.9.0

SpeechRecognizer(SpeechConfig, AutoDetectSourceLanguageConfig)

Creates a new instance of SpeechRecognizer that determines the source language from a list of options. Added in 1.9.0

SpeechRecognizer(SpeechConfig, SourceLanguageConfig, AudioConfig)

Creates a new instance of SpeechRecognizer. Added in 1.9.0

SpeechRecognizer(SpeechConfig, SourceLanguageConfig)

Creates a new instance of SpeechRecognizer. Added in 1.9.0

SpeechRecognizer(SpeechConfig, String, AudioConfig)

Creates a new instance of SpeechRecognizer configured to receive speech in a particular language from an audio source specified in an AudioConfig object. Added in 1.9.0

SpeechRecognizer(SpeechConfig, String)

Creates a new instance of SpeechRecognizer configured to receive speech in a particular language. Added in 1.9.0

SpeechRecognizer(SpeechConfig)

Creates a new instance of SpeechRecognizer configured to receive speech from the default microphone.

Fields

disposed

disposed is a flag used to indicate if object is disposed.

(Inherited from Recognizer)
gch

GC handle for callbacks for context.

(Inherited from Recognizer)
isDisposing

Indicates whether the object is currently being disposed.

(Inherited from Recognizer)
pointerHandle

Internal for logging.

(Inherited from Recognizer)
recognizerLock

recognizerLock is used to synchronize access to objects member variables from multiple threads

(Inherited from Recognizer)

Properties

AuthorizationToken

Gets or sets authorization token used to communicate with the service.

Note: Your code needs to ensure that the authorization token is valid. Before the authorization token expires, your code needs to refresh it by calling this setter with a new valid token. Otherwise, the recognizer will produce errors during recognition.

EndpointId

Gets the endpoint ID of a custom speech model to use for speech recognition.

OutputFormat

Gets the output format setting.

Properties

The collection of properties and their values defined for this SpeechRecognizer. Note: The property collection is only valid until the recognizer owning this Properties is disposed or finalized.

SpeechRecognitionLanguage

Gets the language name that was set when the recognizer was created.

Methods

Dispose()

Dispose of associated resources.

(Inherited from Recognizer)
Dispose(Boolean)

This method performs cleanup of resources. The Boolean parameter disposing indicates whether the method is called from Dispose() (if disposing is true) or from the finalizer (if disposing is false). Derived classes should override this method to dispose resource if needed.

(Inherited from Recognizer)
Finalize()
RecognizeOnceAsync()

Starts speech recognition as an asynchronous operation.

StartContinuousRecognitionAsync()

Starts speech recognition on a continuous audio stream as an asynchronous operation, until StopContinuousRecognitionAsync() is called. You must subscribe to events to receive recognition results.

StartKeywordRecognitionAsync(KeywordRecognitionModel)

Configures the recognizer with the given keyword model. After calling this method, the recognizer is listening for the keyword to start the recognition. Call StopKeywordRecognitionAsync() to end the keyword initiated recognition. You must subscribe to events to receive recognition results.

StopContinuousRecognitionAsync()

Stops a running recognition operation as soon as possible and immediately requests a result based on the the input that has been processed so far. This works for all recognition operations, not just continuous ones, and facilitates the use of push-to-talk or "finish now" buttons for manual audio endpointing.

StopKeywordRecognitionAsync()

Ends the keyword initiated recognition.

Events

Canceled

The event Canceled signals that the speech recognition was canceled.

Recognized

The event Recognized signals that a final recognition result is received.

Recognizing

The event Recognizing signals that an intermediate recognition result is received.

SessionStarted

Defines event handler for session started event.

(Inherited from Recognizer)
SessionStopped

Defines event handler for session stopped event.

(Inherited from Recognizer)
SpeechEndDetected

Defines event handler for speech end detected event.

(Inherited from Recognizer)
SpeechStartDetected

Defines event handler for speech start detected event.

(Inherited from Recognizer)

Applies to