Note

Please see Azure Cognitive Services for Speech documentation for the latest supported speech solutions.

Initialize and Manage the Speech Synthesizer (Microsoft.Speech)

The SpeechSynthesizer class provides control over the text-to-speech (TTS) functionality in the Microsoft Speech Platform Runtime 11. You can initialize a SpeechSynthesizer instance using a constructor on the class, as follows:

SpeechSynthesizer synth = new SpeechSynthesizer();

The following describes how you can use the SpeechSynthesizer class to generate and control speech output.

Select a speaking voice

A voice is an installed Runtime Language for speech synthesis. You can download and install any of 26 Runtime Languages to generate speech in a particular language. See Microsoft Speech Platform SDK 11 Requirements and Installation.

When you initialize a new SpeechSynthesizer instance, it uses the default system voice. To select another installed speaking voice, call the SelectVoice(String) or SelectVoiceByHints() methods. The following gives an example for using SelectVoiceByHints(VoiceGender).

// Initialize a new instance of the SpeechSynthesizer.
SpeechSynthesizer synth = new SpeechSynthesizer();

// Select a voice that matches a specific gender.  
synth.SelectVoiceByHints(VoiceGender.Female);

To get information about which voices are installed and the attributes they possess, use the GetInstalledVoices() method and the VoiceInfo class. The following code excerpt shows some of the information that is available about installed voices.

// Initialize a new instance of the SpeechSynthesizer.
SpeechSynthesizer synth = new SpeechSynthesizer();

// Output information about all of the installed voices. 
foreach (InstalledVoice voice in synth.GetInstalledVoices())
{
  VoiceInfo info = voice.VoiceInfo;

  Console.WriteLine(" Name:          " + info.Name);
  Console.WriteLine(" Culture:       " + info.Culture);
  Console.WriteLine(" Age:           " + info.Age);
  Console.WriteLine(" Gender:        " + info.Gender);
  Console.WriteLine(" Description:   " + info.Description);
  Console.WriteLine(" ID:            " + info.Id);
}

See the GetInstalledVoices() method and the VoiceInfo class for more information.

Control voice characteristics

One way to control voice characteristics is to select a speaking voice that has certain attributes, such as language-culture, age, and gender. You can also set the Rate, Volume, and TtsVolume properties on the SpeechSynthesizer class to control of the speaking rate and output volume for a speaking voice.

For control over the SpeechSynthesizer's pronunciation of specific words, you can create a lexicon that defines words and their pronunciations and add the lexicon to a SpeechSynthesizer instance using the AddLexicon(Uri, String) method. The SpeechSynthesizer instance will use the pronunciations that the lexicon defines. The following example sets the speaking rate and output volume for a new instance of SpeechSynthesizer.

SpeechSynthesizer synth = new SpeechSynthesizer();

// Set the speaking rate and volume.
synth.Rate = -2;
synth.Volume = 60;

The PromptBuilder class also provides methods that you can use to control speech output. See Control Voice Attributes (Microsoft.Speech) for more information and examples.

Configure speech output

You can configure the output of the SpeechSynthesizer to generate speech audio to a WAV stream, a WAV file, an audio stream, or to the default audio output device on the system. Use any of the methods on the SpeechSynthesizer class whose name begins with "SetOutputTo". When you set the output to WAV file, you will typically want to create an instance of SoundPlayer to play back the speech output. The following shows an example of setting the output to a WAV file.

SpeechSynthesizer synth = new SpeechSynthesizer();

// Configure the audio output. 
synth.SetOutputToWaveFile(@"C:\test\Rate.wav");

// Create a SoundPlayer instance to play the output audio file.
System.Media.SoundPlayer m_SoundPlayer =
  new System.Media.SoundPlayer(@"C:\test\Rate.wav");

// Speak a text string synchronously and play back the output file.
synth.Speak("This example speaks a string to a WAV file.");
m_SoundPlayer.Play(); 

Register for events and author handlers

The SpeechSynthesizer class includes events that inform a speech application that the SpeechSynthesizer encountered a specific feature in a prompt, as reported by the SpeakProgressEventArgs and BookmarkReachedEventArgs classes.To get information about the beginning and end of the speaking of a prompt by the SpeechSynthesizer, use the SpeakStartedEventArgs and SpeakCompletedEventArgs classes. The following example shows how to add a handler for the SpeakProgress event and how to write code that reports the position of each word in the speech output.

SpeechSynthesizer synth = new SpeechSynthesizer();

// Add a handler for the SpeakProgress event.
synth.SpeakProgress += 
new EventHandler<SpeakProgressEventArgs>(synth_SpeakProgress);

// Write each word and its character position to the console.
static void synth_SpeakProgress(object sender, SpeakProgressEventArgs e)
{
  Console.WriteLine("Speak progress: {0} {1}", e.CharacterPosition, e.Text);
}

See Use Speech Synthesis Events (Microsoft.Speech) for more information.

Start, pause, and cancel speech output

To generate speech from a string or from a Prompt or PromptBuilder object, use the Speak() or the SpeakAsync() methods. To generate speech from SSML markup, use the SpeakSsml(String) or the SpeakSsmlAsync(String) methods. When you use SpeakAsync() or SpeakSsmlAsync(String) the call returns immediately so your application is free to perform other tasks while the speak operation completes.

You can pause and resume the speaking of a prompt using the SpeechSynthesizer’s Pause() and Resume() methods. You can also cancel the speaking of a specific prompt or of all queued prompts including the prompt being spoken using the SpeakAsyncCancel(Prompt) and SpeakAsyncCancelAll() methods, respectively. These methods act on prompts that were started speaking using a SpeakAsync() or SpeakSsmlAsync(String) call. The following example shows the use of Pause(), Resume(), and SpeakAsyncCancelAll().

using System.Threading;

SpeechSynthesizer synth = new SpeechSynthesizer();

// Begin speaking the prompt.
synth.SpeakAsync("The weather forecast for today is partly cloudy with some sun breaks.");

// Speak for 1.5 seconds.
Thread.Sleep(1500);

// Pause the SpeechSynthesizer object for 1.5 seconds.
synth.Pause();
Thread.Sleep(1500);


// Resume speaking for 1.5 seconds.
synth.Resume();

// Cancel the current speak operation and all queued SpeakAsync operations.
synth.SpeakAsyncCancelAll();