I’ve been wanting to try out System.Speech namespace (present in 3.0 and 3.5 .NET Framework) but unfortunately until this weekend I was stuck with a Portuguese version of Windows Vista which prevented me from using Speech features.
Today I finally tried it out and I was stunned! It couldn’t be simpler (at least for a simple example
). I’ll explain in a few lines of code how to use two main features: Speech Synthesis and Speech Recognition.
Speech Synthesis
using System.Speech.Synthesis;
To make your computer to say something is as simple as 2 lines of code can be:
synthesizer = new SpeechSynthesizer();
synthesizer.Speak("Hello World");
This piece of code has one disadvantage: it locks your program until “Hello World” is said. You can achieve the same goal without this problem by using a asynchronous approach.
// Shut up any speaking going on
if (synthesizer.State == SynthesizerState.Speaking)
synthesizer.SpeakAsyncCancel(
synthesizer.GetCurrentlySpokenPrompt());
// Say text (Async to prevent blocking)
if (synthesizer.State == SynthesizerState.Ready)
synthesizer.SpeakAsync("Hello World");
Speech Recognition
using System.Speech.Recognition;
Speech recognition requires a setup with more code, but as simples as synthesis.
sre = new SpeechRecognitionEngine();
/// Recognize from default Microphone
sre.SetInputToDefaultAudioDevice();
/// Grammar with the words you want to recognize
GrammarBuilder grammarBuilder = new GrammarBuilder();
grammarBuilder.Append(
new Choices("start", "stop", "pause", "restart", "go back"));
Grammar customGrammar = new Grammar(grammarBuilder);
sre.UnloadAllGrammars();
sre.LoadGrammar(customGrammar);
/// Handler to be run in each recognition sre.SpeechRecognized +=
new EventHandler<SpeechRecognizedEventArgs>
(sre_SpeechRecognized);
/// Do not stop recognizing
sre.RecognizeAsync(RecognizeMode.Multiple);
You have to program a handler which will be used to handle the recognized text in the way that most pleases you.
void sre_SpeechRecognized(object sender,
SpeechRecognizedEventArgs e){
/// Store recognized text
txbDebug.Text += e.Result.Text + "\r\n";
}
You can download my first Speech application to see it working ![]()







A very nice example of the Speech capabilities on .NET 3.x (that i didn’t even knew about)!
Thanks for the examples :o)
Hugz,
Luís
[...] The least but the last feature I added was support for Microsoft Speech. I added a welcome message, and if you say the name of a country (in English) the map moves into that country (or to be more specific, to the country name that the application understood…). I’ve achieved this by loading a XML file, which I found over the web, that contains the name of many countries (not sure if contains them all). After loading this file the code is pretty similar to my previous post about Microsoft Speech. [...]
Nice example. I was searching for something straight forward like this. Obrigado meu amigo. E saudações do outro lado do mundo. Franco