Monday, September 26, 2005 5:49 PM
by
philsch
PDC Demo Part I: System.Speech.Synthesis
[I apologize for the delay in following up my promise to start blogging about my PDC demo.]
At the PDC I demoed an speech-enabled RSS reader, built on top of the WPF SDK Viewer sample application. The first functionality I showed was a button that when toggled will read the contents of the blog article using Vista's new built-in voice Anna. Under the covers I used a few regular expressions to extract the content from an HTML blog entry. Once I have a text I can simply do the following 2 steps:
- Add a reference to the speech assembly, currently called Speech.dll in the 'Add Reference' dialog (after installing WinFX).
- Import the namespace into my project:
using System.Speech;
- Create a static instance of a SpeechSynthesizer (we only need one instance so for convenience sake I declare a static instance in the application's main class):
public static SpeechSynthesizer MySynthesizer;
- Speak the article text:
MySynthesizer.Speak(new FilePrompt(<path to text file containing the article>));
The FilePrompt class is derived from the Prompt class. It is a convenient helper when the source of Speak() call is in a file on disk.
In our newsgroups (public.microsoft.speech_tech) we often get asked about how to send the output of the speech synthesizer to a WAV file instead of the default audio.
Here's the code snippet that does just that (taken from my demo):
MySynthesizer.SetOutputToWaveFile(<path to WAV file>);
MySynthesizer.Speak("Let's wreck a nice beach.");
MySynthesizer.Speak("It is now " + DateTime.Now.ToShortTimeString());
In my next blog I will show how you can use the Pause() and Resume() methods to control the speaking action. I'll also talk about some of the events raised by the SpeechSyntheizer class.
- Philipp