Click here to Skip to main content
Click here to Skip to main content

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

By , 21 Aug 2008
 

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#
using System.Speech.Synthesis;
// C++
using namespace System::Speech::Synthesis;
' 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#
SpeechSynthesizer speaker = new SpeechSynthesizer();
// C++
public: SpeechSynthesizer speaker;
' 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#
speaker.Rate = 1;
speaker.Volume = 100;
speaker.Speak("Hello world.");
// C++
speaker.Rate = 1;
speaker.Volume = 100;
speaker.Speak("Hello world.");
' 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?

// 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++
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
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)

About the Author

noxia
Unknown
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questionvery nicememberparkavikarthi16 Nov '12 - 19:10 
good code. worked perfect
SuggestionTry thismemberMember 471378519 Oct '12 - 22:15 
I found this article helpful: Speech Synthesizer in WPF[^]
Generalhimemberyousef12377 Jun '12 - 8:45 
very nic Wink | ;) Wink | ;)
GeneralTrying to include emotions by rulemembertolmega0114 Aug '09 - 10:24 
i have written a text to speech synthesiser that uses and application for it voice and I need to include emotions into . is there any suggestion???
QuestionVoices???memberrspercy6030 Jun '09 - 9:53 
I am looking for other voices to install for the system.speech api. I am looking for Dora the Explorer voice. Does anyone have it? and where can I get it if it exists? Or can i get any other voices that kids recognize?
 
rspercy
1 + 1 = 186,440....Depending on the species.

Questioncan you change the wave format that the tts engine creates???memberchristiansummer28 Oct '08 - 3:20 
i am needing to tts to a certain wave format...in specific mono 16bit @ 16khz...
 
thanks for this much easier tutorial...
 
-c
AnswerRe: can you change the wave format that the tts engine creates???membermwganson3 Apr '09 - 7:44 
Use something like this:
 
string pathToWav;
System.Speech.Synthesizer speaker = new System.Speech.Synthesizer();
 
System.Speech.AudioFormat.SpeechAudioFormatInfo formatInfo = new System.Speech.AudioFormat.SpeechAudioFormatInfo(8000,
System.Speech.AudioFormat.AudioBitsPerSample.Eight,
System.Speech.AudioFormat.AudioChannel.Mono);
 
speaker.SetOutputToWaveFile(pathToWav,formatInfo);
 

Of course, you'll want to modify the parameters for your particular format requirements.
Generalsyntax highlighting while readingmemberalgarnims1 Sep '08 - 2:54 
I have seen many of the features that can be found in commercial software in this wonderful example except syntax highlighting while reading. Any idea how this can be done.
 
Thanks,
Marei
GeneralRe: syntax highlighting while readingmembernoxia1 Sep '08 - 4:30 
I am not sure I understand the question. Syntax highlighting?
Gary
GeneralRe: syntax highlighting while readingmemberalgarnims2 Sep '08 - 3:01 
What I mean by highlighting is like what avilable in Text-To-Speech software e.g. naturalreaders or textaloud. When you press Say or Play button it start reading and it will highlight the word that is being uttered. I suppose that there might be some difficulties to synchronize the highlighted word with the word this is being uttered but I am not sure.

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 21 Aug 2008
Article Copyright 2008 by noxia
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid