SpeechRecognizer Class

Definition

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

See also: Get started with speech-to-text

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

Examples

An example to use the speech recognizer from microphone and listen 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);
    }
}

Constructors

SpeechRecognizer(SpeechConfig)

Creates a new instance of SpeechRecognizer 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)

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

SpeechRecognizer(SpeechConfig, AutoDetectSourceLanguageConfig, 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, SourceLanguageConfig, AudioConfig)

Creates a new instance of SpeechRecognizer. 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, 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

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

isDisposing is a flag used to indicate if object is 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/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. User must subscribe to events to receive recognition results.

StartKeywordRecognitionAsync(KeywordRecognitionModel)

Starts speech recognition on a continuous audio stream with keyword spotting as an asynchronous operation, until StopKeywordRecognitionAsync() is called. User must subscribe to events to receive recognition results.

StopContinuousRecognitionAsync()

Stops continuous speech recognition asynchronously.

StopKeywordRecognitionAsync()

Stops continuous speech recognition with keyword spotting as an asynchronous operation.

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