//----------------------------------- end of public functions --------------------------------------// void Start() { try { // get sensor data KinectManager kinectManager = KinectManager.Instance; if (kinectManager && kinectManager.IsInitialized()) { sensorData = kinectManager.GetSensorData(); } if (sensorData == null || sensorData.sensorInterface == null) { throw new Exception("Speech recognition cannot be started, because KinectManager is missing or not initialized."); } if (debugText != null) { debugText.GetComponent<GUIText>().text = "Please, wait..."; } // ensure the needed dlls are in place and speech recognition is available for this interface bool bNeedRestart = false; if (sensorData.sensorInterface.IsSpeechRecognitionAvailable(ref bNeedRestart)) { if (bNeedRestart) { KinectInterop.RestartLevel(gameObject, "SM"); return; } } else { string sInterfaceName = sensorData.sensorInterface.GetType().Name; throw new Exception(sInterfaceName + ": Speech recognition is not supported!"); } // Initialize the speech recognizer string sCriteria = String.Format("Language={0:X};Kinect=True", languageCode); int rc = sensorData.sensorInterface.InitSpeechRecognition(sCriteria, true, false); if (rc < 0) { string sErrorMessage = (new SpeechErrorHandler()).GetSapiErrorMessage(rc); throw new Exception(String.Format("Error initializing Kinect/SAPI: " + sErrorMessage)); } if (requiredConfidence > 0) { sensorData.sensorInterface.SetSpeechConfidence(requiredConfidence); } if (grammarFileName != string.Empty) { // copy the grammar file from Resources, if available //if(!File.Exists(grammarFileName)) { TextAsset textRes = Resources.Load(grammarFileName, typeof(TextAsset)) as TextAsset; if (textRes != null) { string sResText = textRes.text; File.WriteAllText(grammarFileName, sResText); } } // load the grammar file rc = sensorData.sensorInterface.LoadSpeechGrammar(grammarFileName, (short)languageCode, dynamicGrammar); if (rc < 0) { string sErrorMessage = (new SpeechErrorHandler()).GetSapiErrorMessage(rc); throw new Exception("Error loading grammar file " + grammarFileName + ": " + sErrorMessage); } // // test dynamic grammar phrases // AddGrammarPhrase("addressBook", string.Empty, "Nancy Anderson", true, false); // AddGrammarPhrase("addressBook", string.Empty, "Cindy White", false, false); // AddGrammarPhrase("addressBook", string.Empty, "Oliver Lee", false, false); // AddGrammarPhrase("addressBook", string.Empty, "Alan Brewer", false, false); // AddGrammarPhrase("addressBook", string.Empty, "April Reagan", false, true); } instance = this; sapiInitialized = true; //DontDestroyOnLoad(gameObject); if (debugText != null) { debugText.GetComponent<GUIText>().text = "Ready."; } } catch (DllNotFoundException ex) { Debug.LogError(ex.ToString()); if (debugText != null) debugText.GetComponent<GUIText>().text = "Please check the Kinect and SAPI installations."; } catch (Exception ex) { Debug.LogError(ex.ToString()); if (debugText != null) debugText.GetComponent<GUIText>().text = ex.Message; } }
void OnDestroy() { if (sapiInitialized && sensorData != null && sensorData.sensorInterface != null) { // finish speech recognition sensorData.sensorInterface.FinishSpeechRecognition(); } sapiInitialized = false; instance = null; }