Click here to Skip to main content
15,860,861 members
Articles / Programming Languages / Visual C++ 9.0
Article

A Very Easy Introduction to Microsoft .NET Speech Synthesis (VB, C#, C++)

Rate me:
Please Sign up or sign in to vote.
4.23/5 (37 votes)
21 Aug 2008CPOL1 min read 241.8K   15.5K   104   23
A sample application with source code in three .NET languages (Basic, C#, C++) on using the new (.NET 3.0+) System.Speech Class.

Introduction

This code is an introduction on how to use the new System.Speech class in the .NET Framework v3.0 and v3.5. The sample source code includes an application and the source code is provided in all three .NET languages (VB, C++, C#). It is a simple Windows application and shows how to use text to speech and how to export the speech to a WAV file. The source code was written in Visual Studio 2008 using .NET 3.5.

The Code

Namespaces

First, we need to reference the System.Speech assembly from the application. From the Project menu, choose Add Reference. The from the .NET tab, highlight System.Speech and choose OK.

Then add a simple namespace import to your class file (the Form file).

C#
// C#
using System.Speech.Synthesis;
C++
// C++
using namespace System::Speech::Synthesis;
VB.NET
' VB
Imports System.Speech.Synthesis 

Declarations

Next, we need to declare and instantiate a speech object. The class is System.Speech.Synthesis.Speechsynthesizer. This one class has enough properties and methods to speak a string using the default language and voice of the OS. In Microsoft Windows Vista, the default voice is Microsoft Ana. For Windows XP, it's Microsoft Sam. You can install additional voices, but I'll leave that to another article.

C#
// C#
SpeechSynthesizer speaker = new SpeechSynthesizer();
C++
// C++
public: SpeechSynthesizer speaker;
VB.NET
' VB
Dim speaker as New SpeechSynthesizer()

Usage

Finally, we call the Speak or SpeakAsync method of the object passing in a string containing the text we want spoken.

C#
// C#
speaker.Rate = 1;
speaker.Volume = 100;
speaker.Speak("Hello world.");
C++
// C++
speaker.Rate = 1;
speaker.Volume = 100;
speaker.Speak("Hello world.");
VB.NET
' VB
speaker.Rate = 1
speaker.Volume = 100
speaker.Speak("Hello world".)

You can see from the image that I have also included settings for speaking rate and volume. The sample programs also include code for saving the spoken string to a WAV file. Then you can set your system's sounds to the WAV files you create and your computer will sound like Dr. Falkan's. Would you like to play a game?

C#
// CS
speaker.SetOutputToWaveFile("c:\soundfile.wav");
speaker.Speak("Hellow world.");
speaker.SetOutputToDefaultAudioDevice();
//Must remember to reset out device or the next call to speak 
//will try to write to a file
C++
// C++
speaker.SetOutputToWaveFile("c:\soundfile.wav");
speaker.Speak("Hellow world.");
speaker.SetOutputToDefaultAudioDevice();
//Must remember to reset out device or the next call to speak 
//will try to write to a file
VB.NET
' VB
speaker.SetOutputToWaveFile("c:\soundfile.wav")
speaker.Speak("Hellow world.")
speaker.SetOutputToDefaultAudioDevice()
'Must remember to reset out device or the next call to speak 
'will try to write to a file

History

  • 21st August, 2008: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Unknown
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionC# Speech Synthezizer Pin
jlc32117-Jan-14 10:15
jlc32117-Jan-14 10:15 
Questionvery nice Pin
parkavikarthi16-Nov-12 19:10
parkavikarthi16-Nov-12 19:10 
SuggestionTry this Pin
ybonda19-Oct-12 22:15
ybonda19-Oct-12 22:15 
Generalhi Pin
yousef12377-Jun-12 8:45
yousef12377-Jun-12 8:45 
GeneralTrying to include emotions by rule Pin
tolmega0114-Aug-09 10:24
tolmega0114-Aug-09 10:24 
QuestionVoices??? Pin
rspercy6530-Jun-09 9:53
rspercy6530-Jun-09 9:53 
Questioncan you change the wave format that the tts engine creates??? Pin
christiansummer28-Oct-08 3:20
christiansummer28-Oct-08 3:20 
AnswerRe: can you change the wave format that the tts engine creates??? Pin
mwganson3-Apr-09 7:44
mwganson3-Apr-09 7:44 
Generalsyntax highlighting while reading Pin
algarnims1-Sep-08 2:54
algarnims1-Sep-08 2:54 
GeneralRe: syntax highlighting while reading Pin
noxia1-Sep-08 4:30
noxia1-Sep-08 4:30 
GeneralRe: syntax highlighting while reading Pin
algarnims2-Sep-08 3:01
algarnims2-Sep-08 3:01 
GeneralRe: syntax highlighting while reading Pin
EliotB13-Sep-08 1:26
EliotB13-Sep-08 1:26 
AnswerRe: syntax highlighting while reading Pin
Sunil Srivastav6-Jun-11 22:27
Sunil Srivastav6-Jun-11 22:27 
AnswerVoices .. voices in my head! Pin
GWSyZyGy26-Aug-08 10:54
GWSyZyGy26-Aug-08 10:54 
JokeReminds me of... Pin
James Garner (jadaradix)25-Aug-08 10:33
James Garner (jadaradix)25-Aug-08 10:33 
GeneralAdditional Voices / Languages Pin
User 151565621-Aug-08 23:21
User 151565621-Aug-08 23:21 
GeneralRe: Additional Voices / Languages Pin
topcatalpha25-Aug-08 22:41
topcatalpha25-Aug-08 22:41 
GeneralRe: Additional Voices / Languages Pin
User 151565625-Aug-08 22:51
User 151565625-Aug-08 22:51 
GeneralRe: Additional Voices / Languages Pin
noxia26-Aug-08 7:06
noxia26-Aug-08 7:06 
GeneralRe: Additional Voices / Languages Pin
noxia1-Sep-08 4:32
noxia1-Sep-08 4:32 
I have rewritten the samples to this code to use different voices; I just have not updated the article yet. Although I am having a little trouble with the C++ version.
I'll post today.
Gary
GeneralRe: Additional Voices / Languages Pin
Ravi Tejas19-Nov-08 22:25
Ravi Tejas19-Nov-08 22:25 
GeneralRe: Additional Voices / Languages Pin
amck7724-Mar-11 4:36
amck7724-Mar-11 4:36 
QuestionSpeak in spanish Pin
eyanson21-Aug-08 15:57
eyanson21-Aug-08 15:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.