The library makes calls to the Microsoft Translator Service using the classic HttpWebRequest .NET object and processes the REST response.

Its usage is very simple:

SpeechSynthesizer speech = new SpeechSynthesizer(CLIENT_ID, CLIENT_SECRET);

string text = "Have a nice day!";
string language = "en";

speech.Speak(text, language);

The text is automatically translated in the specified language before speaking. This behaivour is controlled by the AutomaticTranslation property, that is set to true by default. If you don't need to translate the text, because you're already passing it in the language you want to use (as in this example), you can speed-up the library setting the AutomaticTranslation property to false. In this way, the specified text is passed as is to the other methods, without performing any translation. 

If you want the library to automatically detect language text, you simply don't specify the language parameter when you call the Speak method (or any other methods that involve text-to-speech):

SpeechSynthesizer speech = new SpeechSynthesizer(CLIENT_ID, CLIENT_SECRET); 
 
string text = "Have a nice day!"; 
speech.Speak(text);    // Performs language detection before text-to-speech

If you prefer, you can obtain only the speech stream from the library, and then reproduce it yourself:

SpeechSynthesizer speech = new SpeechSynthesizer(CLIENT_ID, CLIENT_SECRET);

string text = "Have a nice day!";
string language = "en";

using (Stream stream = speech.GetSpeakStream(text, language))
{    
    using (SoundPlayer player = new SoundPlayer(stream))
        player.PlaySync();
}

There is also a method that allows to retrieve the list of all supported languages:

SpeechSynthesizer speech = new SpeechSynthesizer(CLIENT_ID, CLIENT_SECRET);

string[] languages = speech.GetLanguages();

Console.WriteLine("Found {0} supported languges:", languages.Length);
foreach (var lang in languages)
{
    Console.WriteLine("{0} - {1}", lang, CultureInfo.GetCultureInfo(lang).DisplayName);
}

All the listed methods are blocking, but the library provides also the corresponding asynchronous versions (SpeakAsync, GetSpeakStreamAsync and GetLanguagesAsync). For example:

SpeechSynthesizer speech = new SpeechSynthesizer(CLIENT_ID, CLIENT_SECRET);
speech.SpeakCompleted += new EventHandler<SpeechEventArgs>(speech_SpeakCompleted);

string text = "Have a nice day!";
string language = "en";

speech.SpeakAsync(text, language);    // This method returns immediately

private static void speech_SpeakCompleted(object sender, SpeechEventArgs e)
{
     Console.WriteLine("Operation completed.");
}

Microsoft Translator Service is able to return streams in both WAV and MP3 format, and we can also specifiy the quality of the audio stream to obtain. To support these features, the SpeechSynthesizer class exposes two properties, AudioFormat and AudioQuality:

SpeechSynthesizer speech = new SpeechSynthesizer(CLIENT_ID, CLIENT_SECRET);
speech.AudioFormat = SpeakStreamFormat.MP3;
speech.AudioQuality = SpeakStreamQuality.MaxQuality;

Note that the Speak and SpeakAsync methods internally use the SoundPlayer class to reproduce streams, so they will throw a NotSupportedException if you use them when the AudioFormat property is set to MP3 (because SoundPlayer supports WAVE file only).

You can retrieve just the translation of the text, without speaking or getting audio stream. Take a look to this example:

SpeechSynthesizer speech = new SpeechSynthesizer(CLIENT_ID, CLIENT_SECRET); 

string text = "Today is a beautiful day"; 
string toLanguage = "it";

var translateText = 
         speech.Translate(text, toLanguage);    // Returns "Oggi è una bella giornata"

As always, Translate method has an asynchronous counterpart, TranslateAsync, that raises the TranslateCompleted event when translated text is available (the result is available in the TranslateEventArgs.TranslatedText property).

Finally, if you want to explicitly retrieve the language of the text, you can call the DetectLanguage method (or the asynchronous version DetectLanguageAsync):

SpeechSynthesizer speech = new SpeechSynthesizer(CLIENT_ID, CLIENT_SECRET); 
 
string text = "Have a nice day!";  
var language = speech.DetectLanguage(text);    // Returns "en"

More Information

Last edited May 31, 2012 at 12:35 AM by marcom, version 12

Comments

No comments yet.