 .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 applications. Common examples of software that requires speech are screen readers for the visually impaired and applications that control automated telephone systems. In this article I will demonstrate the basics of speech synthesis using standard .NET framework classes. In future articles I will examine more detailed use of these classes.
The classes used to generate 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 code-readability, also add the following using directive to your code:
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 approximated.
Plain Text to Speech
The first method that we will use to output speech is named, "Speak". This method can be used with a single string parameter containing the text to be spoken. The sound is played synchronously so the commands following the call are not executed until the speech is completed. You can see this by executing the following code in a console application. You should notice that the text, "Finished", does not appear until after the audio ceases.
var synth = new SpeechSynthesizer();
synth.Speak("Hello");
Console.WriteLine("Finished");
Console.ReadLine();
Asynchronous Speech
Often you will wish to output speech asynchronously to allow further processing to continue whilst the audio is played. This is achieved by using the SpeakAsync method. If you execute the following modified example you should see that the completion message appears before the speaking finishes.
var synth = new SpeechSynthesizer();
synth.SpeakAsync("Hello");
Console.WriteLine("Finished");
Console.ReadLine();
|