public void Start() { var _commands = Commands; //if (IncludeCommonWords) //{ // if (string.IsNullOrEmpty(_commands)) _commands = Extensions.GetStringFromResource(typeof(MSSpeechPlugin), "commands.txt"); // if (!string.IsNullOrEmpty(_commands)) _commands += "\n" + Extensions.GetStringFromResource(typeof(MSSpeechPlugin), "commands.txt"); //} if (!string.IsNullOrEmpty(_commands) || IncludeCommonWords) { var gBuilder = new System.Speech.Recognition.GrammarBuilder(); //if (IncludeCommonWords) //{ // gBuilder.AppendDictation(); //} if (!string.IsNullOrEmpty(_commands)) { var commands = new System.Speech.Recognition.Choices(); var array = _commands.Split(new char[] { '\n', '\r' }).Where(x => !string.IsNullOrEmpty(x)).ToArray(); commands.Add(array); gBuilder.Append(commands); } if (IncludeCommonWords) { gBuilder.AppendDictation(); } var grammer = new System.Speech.Recognition.Grammar(gBuilder); recEngine.UnloadAllGrammars(); recEngine.LoadGrammarAsync(grammer); recEngine.RecognizeAsync(System.Speech.Recognition.RecognizeMode.Multiple); } }
/// <summary> /// Starts listening for voice input /// </summary> public bool StartRecognizer() { if (_recognizerRunning) { StopRecognizer(); } if (!_recognizerRunning) { try { // Start asynchronous, continuous speech recognition. _recognizer.RecognizeAsync(System.Speech.Recognition.RecognizeMode.Multiple); _recognizerRunning = true; return(true); } catch { return(false); } } else { return(true); } }
public static void Main(string[] args) { // Obtain a KinectSensor if any are available KinectSensor sensor = (from sensorToCheck in KinectSensor.KinectSensors where sensorToCheck.Status == KinectStatus.Connected select sensorToCheck).FirstOrDefault(); if (sensor == null) { Console.WriteLine( "No Kinect sensors are attached to this computer or none of the ones that are\n" + "attached are \"Connected\".\n" + "Attach the KinectSensor and restart this application.\n" + "If that doesn't work run SkeletonViewer-WPF to better understand the Status of\n" + "the Kinect sensors.\n\n" + "Press any key to continue.\n"); // Give a chance for user to see console output before it is dismissed Console.ReadKey(true); return; } sensor.Start(); // Obtain the KinectAudioSource to do audio capture KinectAudioSource source = sensor.AudioSource; source.EchoCancellationMode = EchoCancellationMode.None; // No AEC for this sample source.AutomaticGainControlEnabled = false; // Important to turn this off for speech recognition System.Speech.Recognition.RecognizerInfo ri = System.Speech.Recognition.SpeechRecognitionEngine.InstalledRecognizers().FirstOrDefault(); using (var recoEngine = new System.Speech.Recognition.SpeechRecognitionEngine(ri.Id)) { // Create the question dictation grammar. System.Speech.Recognition.DictationGrammar customDictationGrammar = new System.Speech.Recognition.DictationGrammar(); customDictationGrammar.Name = "Dictation"; customDictationGrammar.Enabled = true; // Create a SpeechRecognitionEngine object and add the grammars to it. recoEngine.LoadGrammar(customDictationGrammar); recoEngine.SpeechRecognized += (s, sargs) => Console.Write(sargs.Result.Text); using (Stream s = source.Start()) { recoEngine.SetInputToAudioStream(s, new System.Speech.AudioFormat.SpeechAudioFormatInfo(System.Speech.AudioFormat.EncodingFormat.Pcm, 16000, 16, 1, 32000, 2, null)); Console.WriteLine("Dictating. Press ENTER to stop"); recoEngine.RecognizeAsync(System.Speech.Recognition.RecognizeMode.Multiple); Console.ReadLine(); Console.WriteLine("Stopping recognizer ..."); recoEngine.RecognizeAsyncStop(); } } sensor.Stop(); }
// https://github.com/SynHub/syn-speech-samples // https://github.com/SynHub/syn-speech public static void Test() { System.Console.WriteLine("System.Speech needs Microsoft Speech SDK installed (COM-Object)"); System.Console.WriteLine("https://www.microsoft.com/en-us/download/details.aspx?id=10121"); System.Console.WriteLine("Depending on Framework-Version, recognizers installed with language pack are missing."); System.Console.WriteLine("Despite NetStandard, System.Speech is Windows-ONLY !"); System.Console.WriteLine(System.Environment.NewLine); System.Console.WriteLine("Installed recognizers:"); // Searches in the COM-Object defined in HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Recognizers foreach (System.Speech.Recognition.RecognizerInfo ri in System.Speech.Recognition.SpeechRecognitionEngine.InstalledRecognizers()) { System.Console.Write(" - "); // System.Console.WriteLine(ri.Culture.TwoLetterISOLanguageName); System.Console.WriteLine(ri.Culture.IetfLanguageTag); } // Next ri // Create an in-process speech recognizer for the en-US locale. using (System.Speech.Recognition.SpeechRecognitionEngine recognizer = new System.Speech.Recognition.SpeechRecognitionEngine( // new System.Globalization.CultureInfo("en-US") // new System.Globalization.CultureInfo("de-CH") System.Globalization.CultureInfo.InstalledUICulture )) { // Create and load a dictation grammar. recognizer.LoadGrammar(new System.Speech.Recognition.DictationGrammar()); // Add a handler for the speech recognized event. recognizer.SpeechRecognized += new System.EventHandler <System.Speech.Recognition.SpeechRecognizedEventArgs>(OnSpeechRecognized); // Configure input to the speech recognizer. recognizer.SetInputToDefaultAudioDevice(); // recognizer.SetInputToNull(); // recognizer.SetInputToWaveFile(""); // recognizer.SetInputToWaveStream(new System.IO.MemoryStream()); // System.Speech.AudioFormat.SpeechAudioFormatInfo af = // new System.Speech.AudioFormat.SpeechAudioFormatInfo(System.Speech.AudioFormat.EncodingFormat.Pcm, 4000, 12, 1, 12, 0, null); // recognizer.SetInputToAudioStream(new System.IO.MemoryStream(), af); // Start asynchronous, continuous speech recognition. recognizer.RecognizeAsync(System.Speech.Recognition.RecognizeMode.Multiple); // Keep the console window open. while (true) { System.Console.ReadLine(); } // Whend } // End Using recognizer } // End Sub Test
public static void Test() { System.Speech.Recognition.SpeechRecognitionEngine recEngine = new System.Speech.Recognition.SpeechRecognitionEngine(); recEngine.SetInputToDefaultAudioDevice(); System.Speech.Recognition.Choices commands = new System.Speech.Recognition.Choices(); commands.Add(new string[] { "say Hi", "say Hello" }); System.Speech.Recognition.GrammarBuilder gb = new System.Speech.Recognition.GrammarBuilder(); gb.Append(commands); System.Speech.Recognition.Grammar g = new System.Speech.Recognition.Grammar(gb); recEngine.LoadGrammarAsync(g); recEngine.RecognizeAsync(System.Speech.Recognition.RecognizeMode.Multiple); recEngine.SpeechRecognized += OnSpeechRecognized; } // End Sub Test
private void bVoice_Click(object sender, EventArgs e) { if (MonoCompat.IsMono) { Logger.Log(LogType.Warning, "Voice commands are for windows operating systems only"); return; } //if button was already clicked, cancel if (listening) { listening = false; bVoice.ForeColor = System.Drawing.Color.Black; return; } System.Speech.Recognition.SpeechRecognitionEngine engine = new System.Speech.Recognition.SpeechRecognitionEngine(); bVoice.ForeColor = System.Drawing.Color.Aqua; System.Speech.Recognition.Choices commands = new System.Speech.Recognition.Choices(); commands.Add(new string[] { "restart", "shutdown", "status report", "players", "help" }); System.Speech.Recognition.Grammar gr = new System.Speech.Recognition.Grammar(new System.Speech.Recognition.GrammarBuilder(commands)); try { listening = true; engine.RequestRecognizerUpdate(); engine.LoadGrammar(gr); engine.SpeechRecognized += engine_SpeechRecognized; engine.SetInputToDefaultAudioDevice(); engine.RecognizeAsync(System.Speech.Recognition.RecognizeMode.Multiple); engine.Recognize(); } catch { return; } }