This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

.NET 3.0+

Simple Speech Synthesis

When writing software that will be used by the visually impaired or for systems that have only an audio interface, it can be important to synthesise speech. The .NET framework includes standard classes that make this task simple to achieve.

System.Speech.Synthesis Namespace

Sometimes you will need to output speech from your software. Common examples of software that requires speech are screen readers for the visually impaired and automated telephone systems.

In this article I will demonstrate the basics of speech synthesis using .NET framework classes.

The classes required for speech are found in the System.Speech.Synthesis namespace. To allow access to this namespace you must add a reference to System.Speech.dll to your project. To improve readability, add the following using directive:

using System.Speech.Synthesis;

Text to Speech

The simplest method for outputting speech is to use text to speech conversion. This allows a string containing the words to be spoken to be passed to a method of the SpeechSynthesizer class. The string is parsed and converted to a series of phonemes, each a small unit of sound. By playing the phonemes in quick succession, a human voice is simulated.

Plain Text to Speech

The first method that we will use to output speech is Speak. This can be used with a single string parameter containing the text to be spoken. The sound is played synchronously so commands following the call are not executed until the speech is completed. To demonstrate, execute the following code in a console application. You should notice that the text, "Finished", does not appear until the audio ceases.

var synth = new SpeechSynthesizer();

Asynchronous Speech

You can output speech asynchronously to allow processing to continue whilst the audio is played. To do so, use the SpeakAsync method. If you execute the following example you should see that the completion message appears before the audio finishes.

var synth = new SpeechSynthesizer();
11 December 2009