.net 4 introduced a new namespace:
System.Speech. Yes - that means that your computer can talk.
Why would you want it? First of all it's cool -
with less than 10 lines of code your computer can say whatever you want.
Secondly we are living in a new era - the physical keyboard and the mouse are
being replaced by touch screens (even now you are probably reading this post
from your Smartphone). The present brings touch screens and the not so far
future will bring voice control and much more – "There is no such thing as
Science Fiction any more".
So if you are building a new app and you want to
give it some extra features you should start with some voice control. In order
to make the computer listen and recognize that you're saying we'll use
the SpeechRecognitionEngine class.
SpeechRecognitionEngine recognizer = new
SpeechRecognitionEngine();
recognizer.SetInputToDefaultAudioDevice();
recognizer.LoadGrammar(new Grammar(new GrammarBuilder("hello
computer")));
SetInputToDefaultAudioDevice method tells the recognizer to use
the built in audio device in your computer, you can use the
SetInputToAudioStream method to set a custom device.
The last method is the most interesting one: LoadGrammer receives
the input that the user will say to the computer. The SpeechRecognitionEngine
has two events:
recognizer.SpeechDetected()
recognizer.SpeechRecognized()
The SpeechDetected event fires whenever the computer
detects any sound that can be converted to speech (you talking on the phone
near the computer will raise this event). The SpeechRecognized will rise
when the detected speech is matching to the loaded grammer. You can supply a
single string to the LoadGrammer method or you can use the Choises class:
recognizer.LoadGrammar(new Grammar(new Choices(new []
{
"hello computer",
"I am fine how are you", "goodbye"
})));
Clear right?
The next thing we'll do is subscribe to "SpeechRecognized"
event:
recognizer.SpeechRecognized += Speak;
recognizer.RecognizeAsync(RecognizeMode.Multiple);
RecognizeAsync(RecognizeMode.Multiple) means that the recognition won't
stop after the engine detects a speech.
The last thing that's left to do is to make the computer respond
to our input:
private void Speak(object sender,
SpeechRecognizedEventArgs e)
{
SpeechSynthesizer speechSynthesizer =
new
SpeechSynthesizer();
switch (e.Result.Text)
{
case "hello computer":
{
speechSynthesizer.Speak("hello Dennis, how are you?");
break;
}
}
}
Yes, the computer will say this :)
Earlier we talked about voice control. I want to tell my computer
to open chrome browser, soliter or to start some complex calculation. To do so
we'll need to add a new grammer and a matching action in the method that is
subscribed to the SpeechRecognized event:
case "open chrome":
{
Process.Start("chrome");
break;
}
The future is here.