Dictation Recognizer - Unity
21 October 2020
using UnityEngine;
using UnityEngine.Windows.Speech;
public class SpeechRecogniser: MonoBehaviour{protected DictationRecognizer dictationRecognizer;
void Start(){
}
// CALL THIS FUNCTION WHEN YOU WANT TO START LISTENING TO DICTATIONpublic void startDictationRequest(){
// THIS IS REALLY IMPORTANT!!// If you are using the MRTK and have phrase recognition running, then you can't use both at once,// so you need to turn it off with the following lineif (PhraseRecognitionSystem.Status == SpeechSystemStatus.Running){PhraseRecognitionSystem.Shutdown();}
StartDictationEngine();
}
// CALL THIS FUNCTION WHEN YOU WANT TO STOP LISTENING TO DICTATIONpublic void stopDictationRequest(){
// THIS IS REALLY IMPORTANT!!// If you are using the MRTK and turned off phrase recognition to enable dictation// you need to turn it back ON again with the following lineif (PhraseRecognitionSystem.Status != SpeechSystemStatus.Running){PhraseRecognitionSystem.Restart();}
CloseDictationEngine();}
private void DictationRecognizer_OnDictationHypothesis(string text){
// So in here is the hypothesised text. It's quick, like as fast as it's spoken you'll get the string passed in here// but it's slightly less accurate. So test what you get and see if it's close enough,// otherwise do your checking for your phrases in the OnDictationResult method
if(text.Contains("your required phrase here")){do something here}}
private void DictationRecognizer_OnDictationComplete(DictationCompletionCause completionCause){switch (completionCause){case DictationCompletionCause.TimeoutExceeded:case DictationCompletionCause.PauseLimitExceeded:case DictationCompletionCause.Canceled:case DictationCompletionCause.Complete:// Restart requiredCloseDictationEngine();StartDictationEngine();break;case DictationCompletionCause.UnknownError:case DictationCompletionCause.AudioQualityFailure:case DictationCompletionCause.MicrophoneUnavailable:case DictationCompletionCause.NetworkFailure:// ErrorCloseDictationEngine();break;}}
private void DictationRecognizer_OnDictationResult(string text, ConfidenceLevel confidence){
// Debug.Log("Dictation result: " + text);if(text.Contains("in")){messageText.text = "RESULT CONTAINS IN : " + text;}else{// Debug.Log("RESULT does not CONTAIN IN!!!");}}private void DictationRecognizer_OnDictationError(string error, int hresult){messageText.text = "Dictation error : " + error;Debug.Log("Dictation error: " + error);}private void OnApplicationQuit(){CloseDictationEngine();}private void StartDictationEngine(){Debug.Log("StartDictationEngine");dictationRecognizer = new DictationRecognizer();dictationRecognizer.DictationHypothesis += DictationRecognizer_OnDictationHypothesis;dictationRecognizer.DictationResult += DictationRecognizer_OnDictationResult;dictationRecognizer.DictationComplete += DictationRecognizer_OnDictationComplete;dictationRecognizer.DictationError += DictationRecognizer_OnDictationError;dictationRecognizer.Start();}public void CloseDictationEngine(){if (dictationRecognizer != null){dictationRecognizer.DictationHypothesis -= DictationRecognizer_OnDictationHypothesis;dictationRecognizer.DictationComplete -= DictationRecognizer_OnDictationComplete;dictationRecognizer.DictationResult -= DictationRecognizer_OnDictationResult;dictationRecognizer.DictationError -= DictationRecognizer_OnDictationError;if (dictationRecognizer.Status == SpeechSystemStatus.Running){dictationRecognizer.Stop();}dictationRecognizer.Dispose();}}