Partager via


Use SSML to Control Synthesized Speech

You can use the AppendSsmlMarkup(String) method to append a string containing Speech Synthesis Markup Language (SSML) to a prompt. This topic shows simple examples that use a variety of SSML elements in prompts to control speech output. For more information about SSML elements, see Speech Synthesis Markup Language Reference.

Note

The string used as an argument to AppendSsmlMarkup(String) must not include a speak element. You can also append files containing SSML, see AppendSsml().

Select the Speaking Voice

Using the voice element, you can select an installed text-to-speech (TTS) engine to speak the content of a prompt. For more information, see voice Element. You can specify the voice to use by name, or you can let the SpeechSynthesizer object select an installed voice that matches one or more attributes which you specify. The following example uses the xml:lang attribute of the voice element to specify an English speaking voice.

SpeechSynthesizer synth = new SpeechSynthesizer();
PromptBuilder pb = new PromptBuilder();
pb.AppendSsmlMarkup("<voice xml:lang=\"en-US\">");
pb.AppendText("This is an example of a voice that speaks English as spoken in the United States of America.");
pb.AppendSsmlMarkup("</voice>");
synth.Speak(pb);

Note

Speech synthesis is often referred to as text-to-speech or TTS. A voice contains the information that a TTS engine needs to generate speech in a specific language. An TTS engine can use any voice installed on the system to perform speech synthesis. See InstalledVoice for more information.

Control Voice Characteristics

You can use the prosody element to control the pitch, volume, and speaking rate for synthesized speech output. For more information, see prosody Element. The following example creates three strings containing SSML markup that demonstrate control of the pitch, volume, and speaking rate characteristics of a voice. The example creates a PromptBuilder object and appends each of the three strings, and then passes the PromptBuilder as an argument to the Speak(PromptBuilder) method of the SpeechSynthesizer object.

SpeechSynthesizer synth = new SpeechSynthesizer();
PromptBuilder pb1 = new PromptBuilder();
string high = "<prosody pitch=\"x-high\"> This is extra high pitch. </prosody >";
string loud = "<prosody volume=\"x-loud\"> This is extra loud volume. </prosody>";
string slow = "<prosody rate=\"slow\"> This is the slow speaking rate. </prosody>";
pb1.AppendSsmlMarkup(high);
pb1.AppendSsmlMarkup(loud);
pb1.AppendSsmlMarkup(slow);
synth.Speak(pb1);

Append Audio to a Prompt

The audio element can be used to append audio to a prompt. In the following example, the synthesizer speaks “Substitute text” if it cannot find or play the audio file. For more information, see audio Element.

SpeechSynthesizer synth = new SpeechSynthesizer();
PromptBuilder pb2 = new PromptBuilder();
string str2 = "<audio src = \"C:\\song.wav\"> Substitute text </audio>";
pb2.AppendText("And now a song: ");
pb2.AppendSsmlMarkup(str2);
synth.Speak(pb2);

Append Substitute Text to a Prompt

The sub element can be used to provide alternate text to be rendered. In the following example, the synthesizer speaks “First in first out". For more information, see sub Element.

SpeechSynthesizer synth = new SpeechSynthesizer();
PromptBuilder pb3 = new PromptBuilder();
string str3 = "<sub alias=\"First in first out\"> F. I. F. O. </sub>";
pb3.AppendSsmlMarkup(str3);
synth.Speak(pb3);

Specify Pronunciation

You can use the phoneme element to render speech by its phonetic pronunciation. For more information, see phoneme Element. The following example creates a prompt that, when played, renders the Spanish word "llama" approximately as it would be spoken in Mexican Spanish. The SpeechSynthesizer ignores the contents of the phoneme element (llama) and speaks the phonetic spelling contained by the ph attribute.

SpeechSynthesizer synth = new SpeechSynthesizer();
PromptBuilder pb4 = new PromptBuilder();
string str4 = "<phoneme alphabet=\"x-microsoft-ups\" ph=\"J AA M AX\"> llama </phoneme>";
pb4.AppendSsmlMarkup(str4);
synth.Speak(pb4);