C #, Conversations with a computer or System.Speech

Recently, I, a C # programmer, found in my control panel such a feature as Speech Recognition. I changed the language to English, turned it on, and talked with the computer all night. In the morning I decided to write a calculator, of course speaking. Having stuck in .Net libraries found System.Speech. It sounded promising.

There were 3 namespace in the library: image
For speech recognition and synthesis, and for something else not important.

Synthesis


First, let's deal with Synthesis, write a simple notepad with the speak button:
Add textBoxText, and buttonSpeak to the form. In the code we connect System.Speech.Synthesis, and in the form we create an object we

SpeechSynthesizer ss = new SpeechSynthesizer();

hang up the handler on the button, in it the code for reading the phrase: By the way, during asynchronous reading you can change the speed and volume. You can write just Speak (), but then the whole program hangs itself while reading. Now add another button to the form, saveToWav. Here we also adjust the brightness and speed, but before Speak () we write ss.SetOutputToWaveFile (/ * file path * /); Now for the fun part:

ss.Volume = 100;// от 0 до 100
ss.Rate = 0;//от -10 до 10
ss.SpeakAsync(textBoxText.Text);





Recognition


To use this feature you must have Windows 7 / Vista in English

Connect System.Speech.Recognition, and declare the variables in the form:

private SpeechRecognitionEngine sr;

now we will copy another couple of incomprehensible lines of code into the form constructor: in the SpeechRecognized event we will write: Now when pronouncing the phrases “left”, “right”, “up”, “down”, without huge Russian accent, the corresponding message will be displayed. But if you pronounce with great emphasis, our program recognizes several options. There is a SpeechHypothesized event for this. Add its handler: We will also display the message in it: Now our program can recognize the given commands, this is already good, but sometimes there are a lot of such commands, or for example they can change (for example, “open tab number five”), or we want to make a notepad with voice management ... In general, we need to create another object:

sr.SetInputToDefaultAudioDevice();//микрофон
GrammarBuilder grammarBuilder = new GrammarBuilder();
grammarBuilder.Append(new Choices("left", "right", "up", "down"));//добавляем используемые фразы
sr.UnloadAllGrammars();
sr.LoadGrammar(Grammar(grammarBuilder));//загружаем "грамматику"
sr.SpeechRecognized += new EventHandler(SpeechRecognized);//событие речь распознана
sr.RecognizeAsync(RecognizeMode.Multiple);//начинаем распознование




MessageBox.Show("Recognized phrase: " + e.Result.Text);




sr.SpeechHypothesized += new EventHandler(recognizer_SpeechHypothesized);



MessageBox.Show("Hypothesized phrase: " + e.Result.Text);




private DictationGrammar dictationGrammar;

and in the constructor: Now our program can recognize any English text. This feature is also available for French, Spanish, German, Japanese, and Chinese. But unfortunately I don’t speak them ( PS Let me remind you that you need to make the interface English, otherwise a grammar dictionary will not be created!

dictationGrammar = new DictationGrammar();
sr.LoadGrammar(dictationGrammar);





Also popular now: