TranslationRecognizer Class
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Translates speech input into text and synthesized speech in one or more target languages.
public sealed class TranslationRecognizer : Microsoft.CognitiveServices.Speech.Recognizer
type TranslationRecognizer = class
inherit Recognizer
Public NotInheritable Class TranslationRecognizer
Inherits Recognizer
- Inheritance
Examples
This example uses the translation recognizer from a microphone and receives events generated by the recognizer.
public async Task TranslationContinuousRecognitionAsync()
{
// Creates an instance of a speech translation config with your subscription key and region.
// Replace with your own subscription key and service region (e.g., "westus").
var config = SpeechTranslationConfig.FromSubscription("YourSubscriptionKey", "YourServiceRegion");
// Sets source and target languages.
string fromLanguage = "en-US";
config.SpeechRecognitionLanguage = fromLanguage;
config.AddTargetLanguage("de");
// Sets voice name of synthesis output.
const string GermanVoice = "Microsoft Server Speech Text to Speech Voice (de-DE, ChristophNeural)";
config.VoiceName = GermanVoice;
// Creates a translation recognizer using microphone as audio input.
using (var recognizer = new TranslationRecognizer(config))
{
// Subscribes to events.
recognizer.Recognizing += (s, e) =>
{
Console.WriteLine($"RECOGNIZING in '{fromLanguage}': Text={e.Result.Text}");
foreach (var element in e.Result.Translations)
{
Console.WriteLine($" TRANSLATING into '{element.Key}': {element.Value}");
}
};
recognizer.Recognized += (s, e) =>
{
if (e.Result.Reason == ResultReason.TranslatedSpeech)
{
Console.WriteLine($"\nFinal result: Reason: {e.Result.Reason.ToString()}, recognized text in {fromLanguage}: {e.Result.Text}.");
foreach (var element in e.Result.Translations)
{
Console.WriteLine($" TRANSLATING into '{element.Key}': {element.Value}");
}
}
};
recognizer.Synthesizing += (s, e) =>
{
var audio = e.Result.GetAudio();
Console.WriteLine(audio.Length != 0
? $"AudioSize: {audio.Length}"
: $"AudioSize: {audio.Length} (end of synthesis data)");
};
recognizer.Canceled += (s, e) =>
{
Console.WriteLine($"\nRecognition canceled. Reason: {e.Reason}; ErrorDetails: {e.ErrorDetails}");
};
recognizer.SessionStarted += (s, e) =>
{
Console.WriteLine("\nSession started event.");
};
recognizer.SessionStopped += (s, e) =>
{
Console.WriteLine("\nSession stopped event.");
};
// Starts continuous recognition.
// Uses StopContinuousRecognitionAsync() to stop recognition.
Console.WriteLine("Say something...");
await recognizer.StartContinuousRecognitionAsync().ConfigureAwait(false);
do
{
Console.WriteLine("Press Enter to stop");
} while (Console.ReadKey().Key != ConsoleKey.Enter);
// Stops continuous recognition.
await recognizer.StopContinuousRecognitionAsync().ConfigureAwait(false);
}
}
Remarks
See also: Get started with speech translation
Constructors
TranslationRecognizer(EmbeddedSpeechConfig, AudioConfig) |
Creates a translation recognizer using the specified embedded speech translator and audio configuration. |
TranslationRecognizer(EmbeddedSpeechConfig, AutoDetectSourceLanguageConfig, AudioConfig) |
Creates a translation recognizer using the specified embedded speech config, auto detection source language config and audio config. |
TranslationRecognizer(EmbeddedSpeechConfig, AutoDetectSourceLanguageConfig) |
Creates a translation recognizer using the specified embedded speech config and auto detection source language config. |
TranslationRecognizer(EmbeddedSpeechConfig) |
Creates a translation recognizer using the default microphone input for a specified embedded speech configuration. |
TranslationRecognizer(HybridSpeechConfig, AudioConfig) |
Creates a translation recognizer using the specified hybrid speech translator and audio configuration. |
TranslationRecognizer(HybridSpeechConfig) |
Creates a translation recognizer using the default microphone input for a specified hybrid speech configuration. |
TranslationRecognizer(SpeechTranslationConfig, AudioConfig) |
Creates a translation recognizer using the specified speech translator and audio configuration. |
TranslationRecognizer(SpeechTranslationConfig, AutoDetectSourceLanguageConfig, AudioConfig) |
Creates a translation recognizer using the specified speech translator and audio configuration. |
TranslationRecognizer(SpeechTranslationConfig, AutoDetectSourceLanguageConfig) |
Creates a translation recognizer using the specified speech translator and auto detect source language config |
TranslationRecognizer(SpeechTranslationConfig) |
Creates a translation recognizer using the default microphone input for a specified translation configuration. |
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. |
Properties |
The collection of properties and their values defined for this TranslationRecognizer. 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. |
TargetLanguages |
Gets target languages for translation that were set when the recognizer was created. Each language is specified in BCP-47 format. The translation will provide translated text for each of language. |
VoiceName |
Gets the name of output voice if speech synthesis is used. |
Methods
AddTargetLanguage(String) |
Adds a target language for translation. Added in 1.7.0 |
Dispose() |
Dispose of associated resources. (Inherited from Recognizer) |
Dispose(Boolean) |
This method performs cleanup of resources.
The Boolean parameter |
Finalize() | |
RecognizeOnceAsync() |
Starts speech translation as an asynchronous operation. |
RemoveTargetLanguage(String) |
Removes a target language for translation. Added in 1.7.0 |
StartContinuousRecognitionAsync() |
Starts recognition and translation on a continous audio stream, until StopContinuousRecognitionAsync() is called. You must subscribe to events to receive translation 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 to text/synthesis translation 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) |
Synthesizing |
The event Synthesizing signals that a translation synthesis result is received. |