2 out of 2 rated this helpful - Rate this topic

System.Speech.Synthesis Namespace

The N:System.Speech.Synthesis namespace contains classes for initializing and configuring a speech synthesis engine, for creating prompts, for generating speech, for responding to events, and for modifying voice characteristics.

Initialize and Configure

The SpeechSynthesizer class provides access to the functionality of a speech synthesis engine that is installed on the host computer. Installed speech synthesis engines are represented by a voice, for example Microsoft Anna. A SpeechSynthesizer instance initializes to the default voice. To configure a SpeechSynthesizer instance to use one of the other installed voices, call the SelectVoice(String) or SelectVoiceByHints() methods. To get information about which voices are installed, use the GetInstalledVoices() method.

You can route the output of the SpeechSynthesizer to a stream, a file, the default audio device, or to a null device by using one of the methods in the SpeechSynthesizer class whose name begins with “SetOutputTo”.

Create Prompts

Use one the methods of the PromptBuilder class whose name begins with “Append” to build content for prompts from text, Speech Synthesis Markup Language (SSML), files containing text or SSML markup, or prerecorded audio files.

See Constructing a Complex Prompt in the System Speech Programming Guide for .NET Framework 4.0 for more information and examples.

Generate Speech

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. See Speech Synthesis Markup Language Reference for a guide to SSML markup.

You can guide the pronunciation of words by using the AppendTextWithHint() or AppendTextWithPronunciation(String, String) methods, and by adding or removing lexicons for a SpeechSynthesizer instance using the AddLexicon(Uri, String) and RemoveLexicon(Uri) methods.

Respond to Events

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, BookmarkReachedEventArgs, PhonemeReachedEventArgs, and VisemeReachedEventArgs classes.

To get information about the beginning and end of the speaking of a prompt by the SpeechSynthesizer, use the SpeakStartedEventArgs and SpeakCompletedEventArgs classes.

See Using Speech Synthesis Events in the System Speech Programming Guide for .NET Framework 4.0 for more information and examples.

Modify Voice Characteristics

The PromptStyle class and StartStyle(PromptStyle) and AppendText() methods let you modify characteristics of a SpeechSynthesizer voice using Emphasis, Rate, and Volume parameters. To modify characteristics of a voice such as culture, age, and gender, use one of the StartVoice() methods of the PromptBuilder class or the SelectVoiceByHints() methods of the SpeechSynthesizer class.

See Controlling Voice Attributes in the System Speech Programming Guide for .NET Framework 4.0 for more information.

  Class Description
Public class BookmarkReachedEventArgs Returns data from the BookmarkReached event.
Public class FilePrompt Represents a prompt created from a file.
Public class InstalledVoice Contains information about a speech synthesis voice installed in Windows.
Public class PhonemeReachedEventArgs Returns data from the PhonemeReached event.
Public class Prompt Represents information about what can be rendered, either text or an audio file, by the SpeechSynthesizer.
Public class PromptBuilder Creates an empty Prompt object and provides methods for adding content, selecting voices, controlling voice attributes, and controlling the pronunciation of spoken words.
Public class PromptEventArgs Represents the base class for EventArgs classes in the System.Speech.Synthesis namespace.
Public class PromptStyle Defines a style for speaking prompts that consists of settings for emphasis, rate, and volume.
Public class SpeakCompletedEventArgs Returns notification from the SpeakCompleted event.
Public class SpeakProgressEventArgs Returns data from the SpeakProgress event.
Public class SpeakStartedEventArgs Returns notification from the SpeakStarted event.
Public class SpeechSynthesizer Provides access to the functionality of an installed a speech synthesis engine.
Public class StateChangedEventArgs Returns data from the StateChanged event.
Public class VisemeReachedEventArgs Returns data from the VisemeReached event.
Public class VoiceChangeEventArgs Returns data from the VoiceChange event.
Public class VoiceInfo Represents an installed speech synthesis engine.
  Enumeration Description
Public enumeration PromptBreak Enumerates values for intervals of prosodic separation (breaks) between word boundaries.
Public enumeration PromptEmphasis Enumerates values for levels of emphasis in prompts.
Public enumeration PromptRate Enumerates values for the speaking rate of prompts.
Public enumeration PromptVolume Enumerates values for volume levels (loudness) in prompts.
Public enumeration SayAs Enumerates the content types for the speaking of elements such as times, dates, and currency.
Public enumeration SynthesisMediaType Enumerates the types of media files.
Public enumeration SynthesisTextFormat Enumerates the types of text formats that may be used to construct a Prompt object.
Public enumeration SynthesizerEmphasis Enumerates levels of synthesizer emphasis.
Public enumeration SynthesizerState Enumerates values for the state of the SpeechSynthesizer.
Public enumeration VoiceAge Defines the values for the age of a synthesized voice.
Public enumeration VoiceGender Defines the values for the gender of a synthesized voice.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Testing the Gender and Age Settings
$0$0 $0$0 $0$0 $0
/*
 * Tests System.Speech.Synthesis voice and gender settings.
 */
using System.Speech.Synthesis;
using System.Threading;

namespace TestSystemSpeechTTS
{
    class TestSystemSpeechSynthesis
    {
        /*
         * Main2() creates a promptbuilder with two voices and text strings.
         */
        static void Main2(string[] args)
        {
            int sleeptime = 10;
            SpeechSynthesizer s = new SpeechSynthesizer();
            s.Speak("Hello. My name is Microsoft Anna.");
            Thread.Sleep(sleeptime);

            System.Speech.Synthesis.PromptBuilder pb = new System.Speech.Synthesis.PromptBuilder();
            pb.StartVoice(System.Speech.Synthesis.VoiceGender.Male, System.Speech.Synthesis.VoiceAge.Adult);
            pb.AppendText("Hello. This is my adult male voice.");
            pb.EndVoice();

            pb.StartVoice(System.Speech.Synthesis.VoiceGender.Neutral, System.Speech.Synthesis.VoiceAge.Adult);
            pb.AppendText("Hello. This is my adult neutral voice.");
            pb.EndVoice();
            s.Speak(pb);
            Thread.Sleep(sleeptime);

        }
        /*
         * Speak the passed texted using the passed voice attributes
         * 
         * @param text to speak
         * @param gender of voice (Male, Female or Neutral per W3C, MS accepts NotSet as well).
         * @param age of voice (W3C wants a numeric age, MS wants Adult, Child, Senior, Teen or NotSet).
         * @param PromptBuilder to use to build the SSML
         * @param SpeechSynthesizer to use to speak the text
         */
        static void speakit(string text2say, VoiceGender gender, VoiceAge age, PromptBuilder pb, SpeechSynthesizer s)
        {
            /* 
              Note: If you leave off the EndVoice() you get the following exception:
              System.InvalidOperationException was unhandled
              Message=Cannot generate SSML data: Voice element not closed.
             */
            pb.ClearContent();          // Clear the old content, otherwise it will say that too.
            pb.StartVoice(gender, age); // Set the gender and age attributes
            pb.AppendText(text2say);    // Set the text to speak
            pb.EndVoice();              // Add the XML endtag
            s.Speak(pb);                // Speak the text using the PromptBuilder.

        }
        /*
         * Main() - tests the age and gender settings of the TTS system.
         */
        static void Main(string[] args)
        {
            SpeechSynthesizer s = new SpeechSynthesizer();
            s.Speak("Hello. My name is Microsoft Anna.");
            
            System.Speech.Synthesis.PromptBuilder pb = new System.Speech.Synthesis.PromptBuilder();

            speakit("This is my adult male voice.", VoiceGender.Male, VoiceAge.Adult, pb, s);
            speakit("This is my adult female voice.", VoiceGender.Female, VoiceAge.Adult, pb, s);
            speakit("This is my adult neutral voice.", VoiceGender.Neutral, VoiceAge.Adult, pb, s);

            speakit("This is my child male voice.", VoiceGender.Male, VoiceAge.Child, pb, s);
            speakit("This is my child female voice.", VoiceGender.Female, VoiceAge.Child, pb, s);
            speakit("This is my child neutral voice.", VoiceGender.Neutral, VoiceAge.Child, pb, s);

            speakit("This is my senior male voice.", VoiceGender.Male, VoiceAge.Senior, pb, s);
            speakit("This is my senior female voice.", VoiceGender.Female, VoiceAge.Senior, pb, s);
            speakit("This is my senior neutral voice.", VoiceGender.Neutral, VoiceAge.Senior, pb, s);

            speakit("This is my teen male voice.", VoiceGender.Male, VoiceAge.Teen, pb, s);
            speakit("This is my teen female voice.", VoiceGender.Female, VoiceAge.Teen, pb, s);
            speakit("This is my teen neutral voice.", VoiceGender.Neutral, VoiceAge.Teen, pb, s);

        }
    }
    
}
$0