{"id":146,"date":"2020-10-21T03:43:42","date_gmt":"2020-10-21T03:43:42","guid":{"rendered":"https:\/\/lucyestela.com\/dev\/?p=146"},"modified":"2021-03-02T02:59:42","modified_gmt":"2021-03-02T02:59:42","slug":"dictation-recognizer","status":"publish","type":"post","link":"https:\/\/lucyestela.com\/dev\/unity\/dictation-recognizer\/","title":{"rendered":"Dictation Recognizer &#8211; Unity"},"content":{"rendered":"<div class=\"boldgrid-section\">\n<div class=\"container\">\n<div class=\"row\">\n<div class=\"col-md-12 col-xs-12 col-sm-12\">\n<pre class=\"\">using UnityEngine;\r\nusing UnityEngine.Windows.Speech;\r\n\r\npublic class SpeechRecogniser: MonoBehaviour\r\n{\r\nprotected DictationRecognizer dictationRecognizer;\r\n\r\nvoid Start()\r\n{\r\n\r\n}\r\n\r\n\/\/ CALL THIS FUNCTION WHEN YOU WANT TO START LISTENING TO DICTATION\r\npublic void startDictationRequest(){\r\n\r\n\/\/ THIS IS REALLY IMPORTANT!!\r\n\/\/ If you are using the MRTK and have phrase recognition running, then you can't use both at once,\r\n\/\/ so you need to turn it off with the following line\r\nif (PhraseRecognitionSystem.Status == SpeechSystemStatus.Running)\r\n{\r\nPhraseRecognitionSystem.Shutdown();\r\n}\r\n\r\nStartDictationEngine();\r\n\r\n}\r\n\r\n\/\/ CALL THIS FUNCTION WHEN YOU WANT TO STOP LISTENING TO DICTATION\r\npublic void stopDictationRequest(){\r\n\r\n\/\/ THIS IS REALLY IMPORTANT!!\r\n\/\/ If you are using the MRTK and turned off phrase recognition to enable dictation\r\n\/\/ you need to turn it back ON again with the following line\r\nif (PhraseRecognitionSystem.Status != SpeechSystemStatus.Running)\r\n{\r\nPhraseRecognitionSystem.Restart();\r\n}\r\n\r\nCloseDictationEngine();\r\n}\r\n\r\nprivate void DictationRecognizer_OnDictationHypothesis(string text)\r\n{\r\n\r\n\/\/ 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\r\n\/\/ but it's slightly less accurate. So test what you get and see if it's close enough,\r\n\/\/ otherwise do your checking for your phrases in the OnDictationResult method\r\n\r\nif(text.Contains(\"your required phrase here\")){\r\ndo something here\r\n}\r\n}\r\n\r\nprivate void DictationRecognizer_OnDictationComplete(DictationCompletionCause completionCause)\r\n{\r\nswitch (completionCause)\r\n{\r\ncase DictationCompletionCause.TimeoutExceeded:\r\ncase DictationCompletionCause.PauseLimitExceeded:\r\ncase DictationCompletionCause.Canceled:\r\ncase DictationCompletionCause.Complete:\r\n\/\/ Restart required\r\nCloseDictationEngine();\r\nStartDictationEngine();\r\nbreak;\r\ncase DictationCompletionCause.UnknownError:\r\ncase DictationCompletionCause.AudioQualityFailure:\r\ncase DictationCompletionCause.MicrophoneUnavailable:\r\ncase DictationCompletionCause.NetworkFailure:\r\n\/\/ Error\r\nCloseDictationEngine();\r\nbreak;\r\n}\r\n}\r\n\r\nprivate void DictationRecognizer_OnDictationResult(string text, ConfidenceLevel confidence)\r\n{\r\n\r\n\/\/ Debug.Log(\"Dictation result: \" + text);\r\nif(text.Contains(\"in\")){\r\nmessageText.text = \"RESULT CONTAINS IN : \" + text;\r\n}else{\r\n\/\/ Debug.Log(\"RESULT does not CONTAIN IN!!!\");\r\n}\r\n}\r\nprivate void DictationRecognizer_OnDictationError(string error, int hresult)\r\n{\r\nmessageText.text = \"Dictation error : \" + error;\r\nDebug.Log(\"Dictation error: \" + error);\r\n}\r\nprivate void OnApplicationQuit()\r\n{\r\nCloseDictationEngine();\r\n}\r\nprivate void StartDictationEngine()\r\n{\r\nDebug.Log(\"StartDictationEngine\");\r\ndictationRecognizer = new DictationRecognizer();\r\ndictationRecognizer.DictationHypothesis += DictationRecognizer_OnDictationHypothesis;\r\ndictationRecognizer.DictationResult += DictationRecognizer_OnDictationResult;\r\ndictationRecognizer.DictationComplete += DictationRecognizer_OnDictationComplete;\r\ndictationRecognizer.DictationError += DictationRecognizer_OnDictationError;\r\ndictationRecognizer.Start();\r\n}\r\npublic void CloseDictationEngine()\r\n{\r\nif (dictationRecognizer != null)\r\n{\r\ndictationRecognizer.DictationHypothesis -= DictationRecognizer_OnDictationHypothesis;\r\ndictationRecognizer.DictationComplete -= DictationRecognizer_OnDictationComplete;\r\ndictationRecognizer.DictationResult -= DictationRecognizer_OnDictationResult;\r\ndictationRecognizer.DictationError -= DictationRecognizer_OnDictationError;\r\nif (dictationRecognizer.Status == SpeechSystemStatus.Running)\r\n{\r\ndictationRecognizer.Stop();\r\n}\r\ndictationRecognizer.Dispose();\r\n}\r\n}<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>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 DICTATION public void startDictationRequest(){ \/\/ THIS IS REALLY IMPORTANT!! \/\/ If you are using the MRTK and have phrase recognition running, then you can&#8217;t use both at once, &#8230; <a title=\"Dictation Recognizer &#8211; Unity\" class=\"read-more\" href=\"https:\/\/lucyestela.com\/dev\/unity\/dictation-recognizer\/\" aria-label=\"More on Dictation Recognizer &#8211; Unity\">Read more<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"bgseo_title":"","bgseo_description":"","bgseo_robots_index":"index","bgseo_robots_follow":"follow","footnotes":""},"categories":[58],"tags":[34,4],"class_list":["post-146","post","type-post","status-publish","format-standard","hentry","category-unity","tag-dictation","tag-unity"],"_links":{"self":[{"href":"https:\/\/lucyestela.com\/dev\/wp-json\/wp\/v2\/posts\/146","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lucyestela.com\/dev\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lucyestela.com\/dev\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lucyestela.com\/dev\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/lucyestela.com\/dev\/wp-json\/wp\/v2\/comments?post=146"}],"version-history":[{"count":3,"href":"https:\/\/lucyestela.com\/dev\/wp-json\/wp\/v2\/posts\/146\/revisions"}],"predecessor-version":[{"id":231,"href":"https:\/\/lucyestela.com\/dev\/wp-json\/wp\/v2\/posts\/146\/revisions\/231"}],"wp:attachment":[{"href":"https:\/\/lucyestela.com\/dev\/wp-json\/wp\/v2\/media?parent=146"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lucyestela.com\/dev\/wp-json\/wp\/v2\/categories?post=146"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lucyestela.com\/dev\/wp-json\/wp\/v2\/tags?post=146"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}