5,666,132 members and growing! (17,945 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » .NET Framework » Samples     Beginner License: The Code Project Open License (CPOL)

A very easy intro 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++ (C++, VC9.0), C# (C#, C# 3.0), VB (VB, VB 9.0), Windows (Windows, WinXP, Win2003, Vista), .NET (.NET 3.5, .NET, .NET 3.0), Dev

Posted: 21 Aug 2008
Updated: 21 Aug 2008
Views: 7,798
Bookmarked: 45 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
17 votes for this Article.
Popularity: 4.18 Rating: 3.40 out of 5
3 votes, 17.6%
1
4 votes, 23.5%
2
0 votes, 0.0%
3
5 votes, 29.4%
4
5 votes, 29.4%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Introduction

This code is an introduction to 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

Finnaly, 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

License

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

About the Author

noxia



Location: United States United States

Other popular .NET Framework articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 14 of 14 (Total in Forum: 14) (Refresh)FirstPrevNext
Generalcan you change the wave format that the tts engine creates???memberchristiansummer4:20 28 Oct '08  
Generalsyntax highlighting while readingmemberalgarnims3:54 1 Sep '08  
GeneralRe: syntax highlighting while readingmembernoxia5:30 1 Sep '08  
GeneralRe: syntax highlighting while readingmemberalgarnims4:01 2 Sep '08  
GeneralRe: syntax highlighting while readingmemberEliotB2:26 13 Sep '08  
AnswerVoices .. voices in my head!memberGWSyZyGy11:54 26 Aug '08  
JokeReminds me of...memberNeo-Code11:33 25 Aug '08  
GeneralAdditional Voices / LanguagesmemberSHartmann0:21 22 Aug '08  
GeneralRe: Additional Voices / Languagesmembertopcatalpha23:41 25 Aug '08  
GeneralRe: Additional Voices / LanguagesmemberSHartmann23:51 25 Aug '08  
GeneralRe: Additional Voices / Languagesmembernoxia8:06 26 Aug '08  
GeneralRe: Additional Voices / Languagesmembernoxia5:32 1 Sep '08  
GeneralRe: Additional Voices / LanguagesmemberRavi Tejas23:25 19 Nov '08  
QuestionSpeak in spanishmembereyanson16: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:
Copyright 2008 by noxia
Everything else Copyright © CodeProject, 1999-2008
Web16 | Advertise on the Code Project