Click here to Skip to main content
6,291,722 members and growing! (13,816 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » .NET Framework » Samples     Beginner License: The Code Project Open License (CPOL)

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

By noxia

A sample application with source code in three .NET languages (Basic, C#, C++) on using the new (.NET 3.0+) System.Speech Class.
C# 3.0, VB 9.0, VC9.0WinXP, Win2003, Vista, .NET 3.0, .NET 3.5, Dev
Posted:21 Aug 2008
Views:17,671
Bookmarked:58 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
18 votes for this article.
Popularity: 4.29 Rating: 3.42 out of 5
3 votes, 16.7%
1
4 votes, 22.2%
2

3
6 votes, 33.3%
4
5 votes, 27.8%
5

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


Member

Location: United States United States

Other popular .NET Framework articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 16 of 16 (Total in Forum: 16) (Refresh)FirstPrevNext
GeneralVoices??? Pinmemberrspercy6010:53 30 Jun '09  
Generalcan you change the wave format that the tts engine creates??? Pinmemberchristiansummer4:20 28 Oct '08  
GeneralRe: can you change the wave format that the tts engine creates??? Pinmembermwganson8:44 3 Apr '09  
Generalsyntax highlighting while reading Pinmemberalgarnims3:54 1 Sep '08  
GeneralRe: syntax highlighting while reading Pinmembernoxia5:30 1 Sep '08  
GeneralRe: syntax highlighting while reading Pinmemberalgarnims4:01 2 Sep '08  
GeneralRe: syntax highlighting while reading PinmemberEliotB2:26 13 Sep '08  
AnswerVoices .. voices in my head! PinmemberGWSyZyGy11:54 26 Aug '08  
JokeReminds me of... PinmemberNeo-Code11:33 25 Aug '08  
GeneralAdditional Voices / Languages PinmemberSHartmann0:21 22 Aug '08  
GeneralRe: Additional Voices / Languages Pinmembertopcatalpha23:41 25 Aug '08  
GeneralRe: Additional Voices / Languages PinmemberSHartmann23:51 25 Aug '08  
GeneralRe: Additional Voices / Languages Pinmembernoxia8:06 26 Aug '08  
GeneralRe: Additional Voices / Languages Pinmembernoxia5:32 1 Sep '08  
GeneralRe: Additional Voices / Languages PinmemberRavi Tejas23:25 19 Nov '08  
QuestionSpeak in spanish Pinmembereyanson16:57 21 Aug '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 21 Aug 2008
Editor: Deeksha Shenoy
Copyright 2008 by noxia
Everything else Copyright © CodeProject, 1999-2009
Web20 | Advertise on the Code Project