This program is written by Gary Wee.
Date: 2022 Jul 26
This program uses System.Speech
Nuget package for speech recognition and speech synthesis, it is based on .NET 6.
For older .NET version, you have to select the System.Speech
reference in "Add Reference >> Aseemblies".
Remark: System.Speech
is different from Microsoft.Speech
.
- Set Speech Synthesizer (Text-to-Speech)
You can ask the computer to speak a string using the voice installed in your computer.
How to get installed voice >> Microsoft Docs
SpeechSynthesizer speaker = new SpeechSynthesizer();
speaker.SelectVoice("Microsoft Zira Desktop"); //David, Zira, Hanhan, Huihui
speaker.SpeakAsync("hello gary, how are you doing?");
- Set Speech Recognition Engine (Speech-to-Text)
You can add some words intoChoices
, and append all the Choices intoGrammarBuilder
, then convert it intoGrammar
.
The SpeechRecognitionEngine can load the grammar you created usingLoadGrammar()
.
Remember to plug in your microphone so thatSetInputToDefaultAudioDevice()
can search the input device.
Remark:SpeechRecognitionEngine
is different fromSpeechRecognizer
.
SpeechRecognitionEngine recognizer = new SpeechRecognitionEngine();
Choices choice_01 = new Choices(new string[] { "謝謝", "哈咯", "現在幾點", "我是克禮", "我好累", "看影片", "谷歌搜尋", "今晚我想來點", "再見" });
Choices choice_02 = new Choices(new string[] { "Thanks", "Hello", "What time is it", "I am Gary", "I am tired", "Watch YouTube", "Google Search", "Tonight I would like to have", "Bye" });
GrammarBuilder grammar_builder = new GrammarBuilder();
grammar_builder.Append(choice_01);
grammar_builder.Append(choice_02);
grammar_builder.Culture = recognizer.RecognizerInfo.Culture;
Grammar grammar = new Grammar(grammar_builder);
recognizer.LoadGrammar(grammar);
recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);
recognizer.SetInputToDefaultAudioDevice();
recognizer.RecognizeAsync(RecognizeMode.Multiple);