void SetCommandMode() { int grammar = 1; // 認識させたいコマンド string[] commands = new string[] { "Hello", "Good", "Bad", }; // 認識させたいコマンドを解析する var sts = recognition.BuildGrammarFromStringList(grammar, commands, null); if (sts < pxcmStatus.PXCM_STATUS_NO_ERROR) { throw new Exception("コマンドの解析に失敗しました"); } // 認識させたいコマンドを登録する sts = recognition.SetGrammar(grammar); if (sts < pxcmStatus.PXCM_STATUS_NO_ERROR) { throw new Exception("コマンドの設定に失敗しました"); } }
/// <summary> /// Configures the speech recognition. /// </summary> private bool ConfigureSpeechRecognition() { /* Create the AudioSource instance */ source = session.CreateAudioSource(); /* Set audio volume to 0.2 */ source.SetVolume(0.2f); /* Set Audio Source */ source.SetDevice(sourceDeviceInfo[_activeSource]); /* Set Module */ PXCMSession.ImplDesc mdesc = new PXCMSession.ImplDesc(); mdesc.iuid = modulesIuID[_activeModule]; pxcmStatus sts = session.CreateImpl <PXCMSpeechRecognition>(out sr); if (sts >= pxcmStatus.PXCM_STATUS_NO_ERROR) { /* Configure */ PXCMSpeechRecognition.ProfileInfo pinfo; sr.QueryProfile(_activeLanguage, out pinfo); sr.SetProfile(pinfo); /* Set Command/Control or Dictation */ if (SpeechModuleMode == SpeechModuleModeType.CommandControl) { string[] cmds = Commands; if (cmds != null && cmds.GetLength(0) != 0) { // voice commands available, use them sr.BuildGrammarFromStringList(1, cmds, null); sr.SetGrammar(1); } else { Debug.Log("Speech Command List Empty!"); SetError(SpeechManagerErrorType.VoiceThreadError_CommandsListEmpty); //Switch to dictaction mode //SpeechModuleMode = SpeechModuleModeType.Dictation; //sr.SetDictation(); } } else { sr.SetDictation(); } } else { Debug.Log("VoiceThreadError - InitFailed - CreateImpl!"); SetError(SpeechManagerErrorType.VoiceThreadError_InitFailed_CreateImpl); return(false); } return(true); }
private bool ConfigureSpeechRecognition() { /* Create the AudioSource instance */ source = session.CreateAudioSource(); /* Set audio volume to 0.2 */ source.SetVolume(0.2f); /* Set Audio Source */ source.SetDevice(sourceDeviceInfo[_activeSource]); pxcmStatus sts = session.CreateImpl <PXCMSpeechRecognition>(out sr); if (sts >= pxcmStatus.PXCM_STATUS_NO_ERROR) { /* Configure */ PXCMSpeechRecognition.ProfileInfo pinfo; sr.QueryProfile(_activeLanguage, out pinfo); sr.SetProfile(pinfo); /* Set Command/Control or Dictation */ string[] cmds = new String[4] { "Create", "Save", "Load", "Run" }; if (cmds != null && cmds.GetLength(0) != 0) { // voice commands available, use them sr.BuildGrammarFromStringList(1, cmds, null); sr.SetGrammar(1); } } else { Debug.Log("VoiceThreadError - InitFailed - CreateImpl!"); return(false); } return(true); }
public void DoIt(MainForm form1, PXCMSession session) { form = form1; /* Create the AudioSource instance */ source = session.CreateAudioSource(); if (source == null) { CleanUp(); form.PrintStatus("Stopped"); return; } /* Set audio volume to 0.2 */ source.SetVolume(0.2f); /* Set Audio Source */ source.SetDevice(form.GetCheckedSource()); /* Set Module */ PXCMSession.ImplDesc mdesc = new PXCMSession.ImplDesc(); mdesc.iuid = form.GetCheckedModule(); pxcmStatus sts = session.CreateImpl <PXCMSpeechRecognition>(out sr); if (sts >= pxcmStatus.PXCM_STATUS_NO_ERROR) { /* Configure */ PXCMSpeechRecognition.ProfileInfo pinfo; sr.QueryProfile(form.GetCheckedLanguage(), out pinfo); sr.SetProfile(pinfo); ///////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////// /* Set Command/Control or Dictation */ if (form.IsCommandControl()) { string[] cmds = form.GetCommands(); if (form.g_file != null && form.g_file.Length != 0) { if (form.g_file.EndsWith(".list")) { form.FillCommandListConsole(form.g_file); cmds = form.GetCommands(); if (cmds.GetLength(0) == 0) { form.PrintStatus("Command List Load Errors"); } } // input Command/Control grammar file available, use it if (!SetGrammarFromFile(form.g_file)) { form.PrintStatus("Can not set Grammar From File."); CleanUp(); return; } ; } else if (cmds != null && cmds.GetLength(0) != 0) { // voice commands available, use them sts = sr.BuildGrammarFromStringList(1, cmds, null); sts = sr.SetGrammar(1); } else { form.PrintStatus("No Command List. Dictation instead."); if (form.v_file != null && form.v_file.Length != 0) { SetVocabularyFromFile(form.v_file); } sts = sr.SetDictation(); } } else { if (form.v_file != null && form.v_file.Length != 0) { SetVocabularyFromFile(form.v_file); } sts = sr.SetDictation(); } if (sts < pxcmStatus.PXCM_STATUS_NO_ERROR) { form.PrintStatus("Can't start recognition."); CleanUp(); return; } /* Initialization */ form.PrintStatus("Init Started"); form.PutLabel1Text("初期化中..."); PXCMSpeechRecognition.Handler handler = new PXCMSpeechRecognition.Handler(); handler.onRecognition = OnRecognition; handler.onAlert = OnAlert; sts = sr.StartRec(source, handler); if (sts >= pxcmStatus.PXCM_STATUS_NO_ERROR) { form.PrintStatus("Init OK"); // form.PutLabel1Text("認識中..."); form.PutLabel1Text("マイクに向かって話してください"); /* Wait until the stop button is clicked */ while (!form.IsStop()) { System.Threading.Thread.Sleep(5); } sr.StopRec(); } else { form.PrintStatus("Failed to initialize"); } } else { form.PrintStatus("Init Failed"); } CleanUp(); form.PrintStatus("Stopped"); }
static void SetupRecognizer(PXCMSession session) { PXCMAudioSource.DeviceInfo dinfo = null; if (session != null) { #region Audio Source // session is a PXCMSession instance. source = session.CreateAudioSource(); // Scan and Enumerate audio devices source.ScanDevices(); for (int d = source.QueryDeviceNum() - 1; d >= 0; d--) { source.QueryDeviceInfo(d, out dinfo); // Select one and break out of the loop break; } if (dinfo != null) { // Set the active device source.SetDevice(dinfo); } #endregion #region Recognizer Instance pxcmStatus sts = session.CreateImpl <PXCMSpeechRecognition>(out sr); if (sts < pxcmStatus.PXCM_STATUS_NO_ERROR) { return; } PXCMSpeechRecognition.ProfileInfo pinfo; sr.QueryProfile(0, out pinfo); sr.SetProfile(pinfo); //sr.SetDictation(); #endregion #region Grammar Perintah = new Dictionary <string, Module>(); // sr is a PXCMSpeechRecognition instance. using (var data = new JONGOS_DBEntities()) { var listCommand = from c in data.Modules orderby c.ID select c; foreach (var item in listCommand.Distinct()) { Perintah.Add(item.VoiceCommand, item); } } List <string> cmds = new List <string>(); foreach (var cmd in Perintah.Keys) { cmds.Add(cmd); } // Build the grammar. sr.BuildGrammarFromStringList(1, cmds.ToArray(), null); // Set the active grammar. sr.SetGrammar(1); #endregion #region recognition Event // Set handler PXCMSpeechRecognition.Handler handler = new PXCMSpeechRecognition.Handler(); handler.onRecognition = OnRecognition; //handler.onAlert = OnAlert; // sr is a PXCMSpeechRecognition instance pxcmStatus stsrec = sr.StartRec(source, handler); if (stsrec < pxcmStatus.PXCM_STATUS_NO_ERROR) { Console.WriteLine("Recognizer error!"); } #endregion } }