AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
SpeechInputSource.cs
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License. See LICENSE in the project root for license information.
3 
4 using System;
5 using UnityEngine;
6 
7 #if UNITY_WSA || UNITY_STANDALONE_WIN
8 using UnityEngine.Windows.Speech;
9 #endif
10 
11 namespace HoloToolkit.Unity.InputModule
12 {
23  public partial class SpeechInputSource : BaseInputSource
24  {
28  [Tooltip("Keywords are persistent across all scenes. This Speech Input Source instance will not be destroyed when loading a new scene.")]
29  public bool PersistentKeywords;
30 
31  // This enumeration gives the manager two different ways to handle the recognizer. Both will
32  // set up the recognizer and add all keywords. The first causes the recognizer to start
33  // immediately. The second allows the recognizer to be manually started at a later time.
34  public enum RecognizerStartBehavior { AutoStart, ManualStart }
35 
36  [Tooltip("Whether the recognizer should be activated on start.")]
38 
39  [Tooltip("The keywords to be recognized and optional keyboard shortcuts.")]
41 
42 #if UNITY_WSA || UNITY_STANDALONE_WIN
43 
44  [Tooltip("The confidence level for the keyword recognizer.")]
45  // The serialized data of this field will be lost when switching between platforms and re-serializing this class.
46  [SerializeField]
47  private ConfidenceLevel recognitionConfidenceLevel = ConfidenceLevel.Medium;
48 
49  private KeywordRecognizer keywordRecognizer;
50 
51  #region Unity Methods
52 
53  protected virtual void Start()
54  {
55  if (PersistentKeywords)
56  {
57  gameObject.DontDestroyOnLoad();
58  }
59 
60  int keywordCount = Keywords.Length;
61  if (keywordCount > 0)
62  {
63  var keywords = new string[keywordCount];
64 
65  for (int index = 0; index < keywordCount; index++)
66  {
67  keywords[index] = Keywords[index].Keyword;
68  }
69 
70  keywordRecognizer = new KeywordRecognizer(keywords, recognitionConfidenceLevel);
71  keywordRecognizer.OnPhraseRecognized += KeywordRecognizer_OnPhraseRecognized;
72 
73  if (RecognizerStart == RecognizerStartBehavior.AutoStart)
74  {
75  keywordRecognizer.Start();
76  }
77  }
78  else
79  {
80  Debug.LogError("Must have at least one keyword specified in the Inspector on " + gameObject.name + ".");
81  }
82  }
83 
84  protected virtual void Update()
85  {
86  if (keywordRecognizer != null && keywordRecognizer.IsRunning)
87  {
88  ProcessKeyBindings();
89  }
90  }
91 
92  protected virtual void OnDestroy()
93  {
94  if (keywordRecognizer != null)
95  {
96  StopKeywordRecognizer();
97  keywordRecognizer.OnPhraseRecognized -= KeywordRecognizer_OnPhraseRecognized;
98  keywordRecognizer.Dispose();
99  }
100  }
101 
102  protected virtual void OnDisable()
103  {
104  if (keywordRecognizer != null)
105  {
106  StopKeywordRecognizer();
107  }
108  }
109 
110  protected virtual void OnEnable()
111  {
112  if (keywordRecognizer != null && RecognizerStart == RecognizerStartBehavior.AutoStart)
113  {
114  StartKeywordRecognizer();
115  }
116  }
117 
118  #endregion // Unity Methods
119 
120  #region Event Callbacks
121 
122  private void KeywordRecognizer_OnPhraseRecognized(PhraseRecognizedEventArgs args)
123  {
124  OnPhraseRecognized(args.confidence, args.phraseDuration, args.phraseStartTime, args.semanticMeanings, args.text);
125  }
126 
127  #endregion // Event Callbacks
128 
133  public void StartKeywordRecognizer()
134  {
135  if (keywordRecognizer != null && !keywordRecognizer.IsRunning)
136  {
137  keywordRecognizer.Start();
138  }
139  }
140 
141  private void ProcessKeyBindings()
142  {
143  for (int index = Keywords.Length; --index >= 0;)
144  {
145  if (Input.GetKeyDown(Keywords[index].KeyCode))
146  {
147  OnPhraseRecognized(recognitionConfidenceLevel, TimeSpan.Zero, DateTime.Now, null, Keywords[index].Keyword);
148  }
149  }
150  }
151 
156  public void StopKeywordRecognizer()
157  {
158  if (keywordRecognizer != null && keywordRecognizer.IsRunning)
159  {
160  keywordRecognizer.Stop();
161  }
162  }
163 
164  protected void OnPhraseRecognized(ConfidenceLevel confidence, TimeSpan phraseDuration, DateTime phraseStartTime, SemanticMeaning[] semanticMeanings, string text)
165  {
166  InputManager.Instance.RaiseSpeechKeywordPhraseRecognized(this, 0, confidence, phraseDuration, phraseStartTime, semanticMeanings, text);
167  }
168 
169 #endif
170 
171  #region Base Input Source Methods
172 
173  public override bool TryGetSourceKind(uint sourceId, out InteractionSourceInfo sourceKind)
174  {
175  sourceKind = InteractionSourceInfo.Voice;
176  return true;
177  }
178 
179  public override bool TryGetPointerPosition(uint sourceId, out Vector3 position)
180  {
181  position = Vector3.zero;
182  return false;
183  }
184 
185  public override bool TryGetPointerRotation(uint sourceId, out Quaternion rotation)
186  {
187  rotation = Quaternion.identity;
188  return false;
189  }
190 
191  public override bool TryGetPointingRay(uint sourceId, out Ray pointingRay)
192  {
193  pointingRay = default(Ray);
194  return false;
195  }
196 
197  public override bool TryGetGripPosition(uint sourceId, out Vector3 position)
198  {
199  position = Vector3.zero;
200  return false;
201  }
202 
203  public override bool TryGetGripRotation(uint sourceId, out Quaternion rotation)
204  {
205  rotation = Quaternion.identity;
206  return false;
207  }
208 
209  public override SupportedInputInfo GetSupportedInputInfo(uint sourceId)
210  {
211  return SupportedInputInfo.None;
212  }
213 
214  public override bool TryGetThumbstick(uint sourceId, out bool isPressed, out Vector2 position)
215  {
216  isPressed = false;
217  position = Vector2.zero;
218  return false;
219  }
220 
221  public override bool TryGetTouchpad(uint sourceId, out bool isPressed, out bool isTouched, out Vector2 position)
222  {
223  isPressed = false;
224  isTouched = false;
225  position = Vector2.zero;
226  return false;
227  }
228 
229  public override bool TryGetSelect(uint sourceId, out bool isPressed, out double pressedAmount)
230  {
231  isPressed = false;
232  pressedAmount = 0.0;
233  return false;
234  }
235 
236  public override bool TryGetGrasp(uint sourceId, out bool isPressed)
237  {
238  isPressed = false;
239  return false;
240  }
241 
242  public override bool TryGetMenu(uint sourceId, out bool isPressed)
243  {
244  isPressed = false;
245  return false;
246  }
247 
248  #endregion // Base Input Source Methods
249  }
250 }
bool PersistentKeywords
Keywords are persistent across all scenes. This Speech Input Source instance will not be destroyed wh...
SpeechInputSource allows you to specify keywords and methods in the Unity Inspector, instead of registering them explicitly in code. This also includes a setting to either automatically start the keyword recognizer or allow your code to start it.
Input Manager is responsible for managing input sources and dispatching relevant events to the approp...
Definition: InputManager.cs:19
SupportedInputInfo
Flags used to indicate which input information is supported by an input source.
override bool TryGetGripRotation(uint sourceId, out Quaternion rotation)
Returns the rotation of the input source, if available. Not all input sources support rotation inform...
override bool TryGetPointerRotation(uint sourceId, out Quaternion rotation)
Returns the rotation of the input source, if available. Not all input sources support rotation inform...
override SupportedInputInfo GetSupportedInputInfo(uint sourceId)
Returns the input info that the input source can provide.
static T Instance
Returns the Singleton instance of the classes type. If no instance is found, then we search for an in...
Definition: Singleton.cs:26
override bool TryGetTouchpad(uint sourceId, out bool isPressed, out bool isTouched, out Vector2 position)
override bool TryGetGrasp(uint sourceId, out bool isPressed)
override bool TryGetMenu(uint sourceId, out bool isPressed)
override bool TryGetSourceKind(uint sourceId, out InteractionSourceInfo sourceKind)
override bool TryGetGripPosition(uint sourceId, out Vector3 position)
Returns the position of the input source, if available. Not all input sources support positional info...
override bool TryGetThumbstick(uint sourceId, out bool isPressed, out Vector2 position)
override bool TryGetSelect(uint sourceId, out bool isPressed, out double pressedAmount)
Base class for an input source.
override bool TryGetPointerPosition(uint sourceId, out Vector3 position)
Returns the position of the input source, if available. Not all input sources support positional info...
override bool TryGetPointingRay(uint sourceId, out Ray pointingRay)
Returns the pointing ray of the input source, if available. Not all input sources support pointing in...