/// <summary> /// Loads speech grammar async. /// </summary> private void LoadSpeechGrammarAsync() { //Set the end silence time out. _speechRecognitionEngine.EndSilenceTimeout = this._completeRecognitionTimeOut; if (_grammars.Count > 0) { //Register handler and load each grammar foreach (Microsoft.Speech.Recognition.Grammar grammar in _grammars) { _pendingLoadSpeechGrammarCounter++; _speechRecognitionEngine.LoadGrammarAsync(grammar); } } else { _waitForLoadGrammarCompleted.Set(); } }
private void Gramatica() { try { sr = new Microsoft.Speech.Recognition.SpeechRecognitionEngine(ci); } catch (Exception ex) { MessageBox.Show("Erro: " + ex.Message); } var gramatica = new Microsoft.Speech.Recognition.Choices(); gramatica.Add(words); var gb = new Microsoft.Speech.Recognition.GrammarBuilder(); gb.Append(gramatica); try { var g = new Microsoft.Speech.Recognition.Grammar(gb); try { sr.RequestRecognizerUpdate(); sr.LoadGrammarAsync(g); sr.SpeechRecognized += Sr_SpeechRecognized; sr.SetInputToDefaultAudioDevice(); ss.SetOutputToDefaultAudioDevice(); sr.RecognizeAsync(Microsoft.Speech.Recognition.RecognizeMode.Multiple); } catch (Exception ex) { MessageBox.Show("Erro: " + ex.Message); } } catch (Exception ex) { MessageBox.Show("Erro: " + ex.Message); } }
void LoadSettings(string configFileName) { var settings = File.ReadAllText(configFileName); jarvisName = Regex.Match(settings, "name: \"(.+?)\"").Groups[1].Value; helloMessage = Regex.Match(settings, "hello_message: \"(.+?)\"").Groups[1].Value; threshold = double.Parse(Regex.Match(settings, "confidence_threshold: (.+?) ", RegexOptions.Compiled).Groups[1].Value.Replace('.', ',')); recognize_language = Regex.Match(settings, "recognize_language: \"(.+?)\"").Groups[1].Value; speech_language = Regex.Match(settings, "speech_language: \"(.+?)\"").Groups[1].Value; #region DaysOfWeek var daysMatch = Regex.Match(settings, "daysofweek:((\\s\"(?<DayName>.+?)\")+)"); var daysCaptures = daysMatch.Groups["DayName"].Captures; var daysList = new List <string>(); foreach (Capture match in daysCaptures) { daysList.Add(match.Value); } daysofweek = daysList.ToArray(); #endregion x_hours = Regex.Match(settings, "x_hours: \"(.+?)\"").Groups[1].Value; x_minutes = Regex.Match(settings, "x_minutes: \"(.+?)\"").Groups[1].Value; recognized = Regex.Match(settings, "recognized: \"(.+?)\"").Groups[1].Value; ignored = Regex.Match(settings, "ignored: \"(.+?)\"").Groups[1].Value; commandsList.Clear(); var commandsMatches = Regex.Matches(settings, "^\"(?<VoiceCommand>.+?)\"\\s*\"(?<CommandType>.+?)\"(?: \"(?<Option>.+?)\")*", RegexOptions.Multiline); foreach (Match m in commandsMatches) { var cmd = new Command(); cmd.VoiceCommand = m.Groups["VoiceCommand"].Value; cmd.Type = m.Groups["CommandType"].Value; cmd.Option = m.Groups["Option"].Value; commandsList.Add(cmd); } var allVoiceCommands = commandsList.Select(c => c.VoiceCommand).ToArray(); commands = new Choices(); commands.Add(allVoiceCommands); gBuilder = new GrammarBuilder(); gBuilder.Append(commands); gBuilder.Culture = new System.Globalization.CultureInfo(recognize_language); if (prev_recognize_language != recognize_language) { recEngine = new Microsoft.Speech.Recognition.SpeechRecognitionEngine(new System.Globalization.CultureInfo(recognize_language)); try { recEngine.SetInputToDefaultAudioDevice(); } catch { RecognizedText_TextBox.Text += "В системе не выбрано устройство записи звука по-умолчанию. Выберите и перезапустите программу."; return; } prev_recognize_language = recognize_language; grammar = new Grammar(gBuilder); recEngine.LoadGrammarAsync(grammar); recEngine.SpeechRecognized += RecEngine_SpeechRecognized; recEngine.SpeechHypothesized += RecEngine_SpeechHypothesized; recEngine.RecognizeAsync(RecognizeMode.Multiple); } var voice = new System.Speech.Synthesis.SpeechSynthesizer().GetInstalledVoices().Where(v => v.VoiceInfo.Name.Contains(speech_language)).ToArray()[0].VoiceInfo.Name; synth.SelectVoice(voice); try { synth.SetOutputToDefaultAudioDevice(); } catch (Exception) { RecognizedText_TextBox.Text += "В системе не выбрано устройство воспроизведения звука по-умолчанию. Выберите и перезапустите программу."; return; } synth.Volume = 100; Say(helloMessage); }