📜 ⬆️ ⬇️

C #, Talk with a computer or System.Speech

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

The library had 3 namespaces: image
For speech recognition and synthesis, and for something else not important.

Synthesis


First, let's deal with Synthesis, write the simplest notebook 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

SpeechSynthesizer ss = new SpeechSynthesizer();

We hang up the handler on the button, in it the code for reading the phrase:
')
ss.Volume = 100;// 0 100
ss.Rate = 0;// -10 10
ss.SpeakAsync(textBoxText.Text);


By the way, during asynchronous reading, you can change the speed and volume. You can write just Speak (), but then the whole program will hang itself for the time of reading. Now add another button to the form, saveToWav. Here we also adjust the speed and speed, but before Speak () we write ss.SetOutputToWaveFile (/ * file path * /);
Now the most interesting:

Recognition


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

Let's connect System.Speech.Recognition, and declare variables in the form:

private SpeechRecognitionEngine sr;

Now we’ll copy a couple more incomprehensible lines of code into the form constructor:

sr.SetInputToDefaultAudioDevice();//
GrammarBuilder grammarBuilder = new GrammarBuilder();
grammarBuilder.Append(new Choices("left", "right", "up", "down"));//
sr.UnloadAllGrammars();
sr.LoadGrammar(Grammar(grammarBuilder));// ""
sr.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(SpeechRecognized);//
sr.RecognizeAsync(RecognizeMode.Multiple);//


in the event SpeechRecognized we write:

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

Now, when pronouncing the phrases “left”, “right”, “up”, “down”, without a huge Russian accent, the corresponding message will be displayed.
But if you say with great emphasis, our program recognizes several options. For this there is a SpeechHypothesized event. Add its handler:

sr.SpeechHypothesized += new EventHandler<SpeechHypothesizedEventArgs>(recognizer_SpeechHypothesized);

We will also display the message in it:

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

Now our program is able to recognize the specified commands, this is not bad, but sometimes there are many such commands, or for example they can change (for example, “open tab number five”), or we want to make a notepad with voice control ...
In general, you need to create another object:

private DictationGrammar dictationGrammar;

and in the constructor:

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


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 about them (

PS Let me remind you that you need to make the English interface language, otherwise the grammar dictionary will not be created!

Source: https://habr.com/ru/post/125432/


All Articles