Compartilhar via


Note

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

Use SSML to Control Synthesized Speech (Microsoft.Speech)

You can use the AppendSsmlMarkup(String) method to append a string containing Speech Synthesis Markup Language (SSML) to a prompt. This topic shows basic 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 (Microsoft.Speech).

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(XmlReader).

Select the Speaking Voice

Using the voice element, you can select an installed voice to speak the content of a prompt. For more information, see voice Element (Microsoft.Speech). 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 a French speaking voice to speak a phrase in French. To pronounce the phrase correctly, a French speaking voice must be installed.

SpeechSynthesizer synth = new SpeechSynthesizer();
PromptBuilder pb = new PromptBuilder();
pb.AppendSsmlMarkup("<voice xml:lang=\"fr-FR\">");
pb.AppendText("Ceci est un exemple d'une voix parlant français.");
pb.AppendSsmlMarkup("</voice>");
synth.Speak(pb);

Note

Speech synthesis is often referred to as text-to-speech or TTS. A voice is an installed Runtime Language that a TTS engine uses to perform speech synthesis in a specific language. 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 (Microsoft.Speech). 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 (Microsoft.Speech).

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 (Microsoft.Speech).

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 (Microsoft.Speech). 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);