SpeechRecognizer SpeechRecognizer SpeechRecognizer Class

Definition

Performs speech recognition from microphone, file, or other audio input streams, and gets transcribed text as result.

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

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 service 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    Recognition 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) SpeechRecognizer(SpeechConfig) SpeechRecognizer(SpeechConfig)

Creates a new instance of SpeechRecognizer.

SpeechRecognizer(SpeechConfig, AudioConfig) SpeechRecognizer(SpeechConfig, AudioConfig) SpeechRecognizer(SpeechConfig, AudioConfig)

Creates a new instance of SpeechRecognizer.

Properties

AuthorizationToken AuthorizationToken AuthorizationToken

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

EndpointId EndpointId EndpointId

Gets the endpoint ID of a customized speech model that is used for speech recognition.

OutputFormat OutputFormat OutputFormat

Gets the output format setting.

Properties Properties Properties

The collection or properties and their values defined for this SpeechRecognizer.

SpeechRecognitionLanguage SpeechRecognitionLanguage SpeechRecognitionLanguage

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

Methods

Dispose() Dispose() Dispose()

Dispose of associated resources.

(Inherited from Recognizer)
Dispose(Boolean) Dispose(Boolean) 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)
RecognizeOnceAsync() RecognizeOnceAsync() RecognizeOnceAsync()

Starts speech recognition, and stops after the first utterance is recognized. The task returns the recognition text as result. Note: RecognizeOnceAsync() returns when the first utterance has been recognized, so it is suitable only for single shot recognition like command or query. For long-running recognition, use StartContinuousRecognitionAsync() instead.

StartContinuousRecognitionAsync() StartContinuousRecognitionAsync() StartContinuousRecognitionAsync()

Starts speech recognition on a continuous audio stream, until StopContinuousRecognitionAsync() is called. User must subscribe to events to receive recognition results.

StartKeywordRecognitionAsync(KeywordRecognitionModel) StartKeywordRecognitionAsync(KeywordRecognitionModel) StartKeywordRecognitionAsync(KeywordRecognitionModel)

Starts speech recognition on a continuous audio stream with keyword spotting, until StopKeywordRecognitionAsync() is called. User must subscribe to events to receive recognition results. Note: Keyword spotting functionality is only available on the Cognitive Services Device SDK. This functionality is currently not included in the SDK itself.

StopContinuousRecognitionAsync() StopContinuousRecognitionAsync() StopContinuousRecognitionAsync()

Stops continuous speech recognition.

StopKeywordRecognitionAsync() StopKeywordRecognitionAsync() StopKeywordRecognitionAsync()

Stops continuous speech recognition with keyword spotting. Note: Key word spotting functionality is only available on the Cognitive Services Device SDK. This functionality is currently not included in the SDK itself.

Events

Canceled Canceled Canceled

The event Canceled signals that the speech recognition was canceled.

Recognized Recognized Recognized

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

Recognizing Recognizing Recognizing

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

SessionStarted SessionStarted SessionStarted

Defines event handler for session started event.

(Inherited from Recognizer)
SessionStopped SessionStopped SessionStopped

Defines event handler for session stopped event.

(Inherited from Recognizer)
SpeechEndDetected SpeechEndDetected SpeechEndDetected

Defines event handler for speech end detected event.

(Inherited from Recognizer)
SpeechStartDetected SpeechStartDetected SpeechStartDetected

Defines event handler for speech start detected event.

(Inherited from Recognizer)

Extension Methods

IsDefined(Object) IsDefined(Object) IsDefined(Object)

Determines if a certain property is defined or not.

IsNull(Object) IsNull(Object) IsNull(Object)

Determines if a certain property is null or not.

IsPrimitive(Object) IsPrimitive(Object) IsPrimitive(Object)

Determines if a certain property is of premitive JSON type.

Applies to