44using Microsoft . MixedReality . Toolkit . Internal . Definitions . InputSystem ;
55using Microsoft . MixedReality . Toolkit . Internal . Interfaces . InputSystem ;
66using Microsoft . MixedReality . Toolkit . Internal . Utilities . Async ;
7+ using Microsoft . MixedReality . Toolkit . Internal . Utilities . Async . AwaitYieldInstructions ;
78using System . Threading . Tasks ;
89using UnityEngine ;
910
@@ -25,49 +26,18 @@ public class DictationInputSource : BaseGenericInputSource
2526 /// </summary>
2627 public static bool IsListening { get ; private set ; } = false ;
2728
28- /// <summary>
29- /// Action that will be associated with a dictation hypothesis input event.
30- /// </summary>
31- public static MixedRealityInputAction HypothesisAction { get ; set ; }
32-
33- /// <summary>
34- /// Action that will be associated with a dictation result input event.
35- /// </summary>
36- public static MixedRealityInputAction ResultAction { get ; set ; }
37-
38- /// <summary>
39- /// Action that will be associated with a dictation complete input event.
40- /// </summary>
41- public static MixedRealityInputAction CompleteAction { get ; set ; }
42-
43- /// <summary>
44- /// Action that will be associated with a dictation error input event.
45- /// </summary>
46- public static MixedRealityInputAction ErrorAction { get ; set ; }
47-
4829#if UNITY_STANDALONE_WIN || UNITY_WSA || UNITY_EDITOR_WIN
4930
5031 /// <summary>
5132 /// Constructor.
5233 /// </summary>
53- /// <param name="hypothesisAction">Action that will be associated with a dictation hypothesis input event</param>
54- /// <param name="resultAction">Action that will be associated with a dictation result input event</param>
55- /// <param name="completeAction">Action that will be associated with a dictation complete input event</param>
56- /// <param name="errorAction">Action that will be associated with a dictation error input event.</param>
57- public DictationInputSource ( MixedRealityInputAction hypothesisAction ,
58- MixedRealityInputAction resultAction ,
59- MixedRealityInputAction completeAction ,
60- MixedRealityInputAction errorAction )
61- : base ( "Dictation" )
34+ public DictationInputSource ( ) : base ( "Dictation" )
6235 {
36+ if ( ! Application . isPlaying ) { return ; }
37+
6338 source = this ;
6439 dictationResult = string . Empty ;
6540
66- HypothesisAction = hypothesisAction ;
67- ResultAction = resultAction ;
68- CompleteAction = completeAction ;
69- ErrorAction = errorAction ;
70-
7141 dictationRecognizer = new DictationRecognizer ( ) ;
7242 dictationRecognizer . DictationHypothesis += DictationRecognizer_DictationHypothesis ;
7343 dictationRecognizer . DictationResult += DictationRecognizer_DictationResult ;
@@ -81,16 +51,17 @@ public DictationInputSource(MixedRealityInputAction hypothesisAction,
8151 Run ( ) ;
8252 }
8353
84- /// <summary>
85- /// Destructor.
86- /// </summary>
87- ~ DictationInputSource ( )
54+ /// <inheritdoc />
55+ public override void Dispose ( )
8856 {
89- dictationRecognizer . DictationHypothesis -= DictationRecognizer_DictationHypothesis ;
90- dictationRecognizer . DictationResult -= DictationRecognizer_DictationResult ;
91- dictationRecognizer . DictationComplete -= DictationRecognizer_DictationComplete ;
92- dictationRecognizer . DictationError -= DictationRecognizer_DictationError ;
93- dictationRecognizer . Dispose ( ) ;
57+ if ( dictationRecognizer != null )
58+ {
59+ dictationRecognizer . DictationHypothesis -= DictationRecognizer_DictationHypothesis ;
60+ dictationRecognizer . DictationResult -= DictationRecognizer_DictationResult ;
61+ dictationRecognizer . DictationComplete -= DictationRecognizer_DictationComplete ;
62+ dictationRecognizer . DictationError -= DictationRecognizer_DictationError ;
63+ dictationRecognizer ? . Dispose ( ) ;
64+ }
9465 }
9566
9667 private static IMixedRealityInputSource source ;
@@ -126,7 +97,7 @@ public DictationInputSource(MixedRealityInputAction hypothesisAction,
12697 /// </summary>
12798 private static AudioClip dictationAudioClip ;
12899
129- private static readonly WaitForFixedUpdate nextUpdate = new WaitForFixedUpdate ( ) ;
100+ private static readonly WaitForUpdate NextUpdate = new WaitForUpdate ( ) ;
130101
131102 private static async void Run ( )
132103 {
@@ -141,10 +112,10 @@ private static async void Run()
141112 if ( ! hasFailed && dictationRecognizer . Status == SpeechSystemStatus . Failed )
142113 {
143114 hasFailed = true ;
144- InputSystem . RaiseDictationError ( source , ErrorAction , "Dictation recognizer has failed!" ) ;
115+ InputSystem . RaiseDictationError ( source , "Dictation recognizer has failed!" ) ;
145116 }
146117
147- await nextUpdate ;
118+ await NextUpdate ;
148119 }
149120 }
150121
@@ -192,7 +163,7 @@ public static async Task StartRecordingAsync(GameObject listener = null, float i
192163
193164 if ( dictationRecognizer . Status == SpeechSystemStatus . Failed )
194165 {
195- InputSystem . RaiseDictationError ( source , ErrorAction , "Dictation recognizer failed to start!" ) ;
166+ InputSystem . RaiseDictationError ( source , "Dictation recognizer failed to start!" ) ;
196167 return ;
197168 }
198169
@@ -201,7 +172,8 @@ public static async Task StartRecordingAsync(GameObject listener = null, float i
201172 textSoFar = new StringBuilder ( ) ;
202173 isTransitioning = false ;
203174#else
204- throw new NotImplementedException ( "Unable to start recording! Dictation is unsupported for this platform." ) ;
175+
176+ Debug . LogError ( "Unable to start recording! Dictation is unsupported for this platform." ) ;
205177#endif // UNITY_STANDALONE_WIN || UNITY_WSA || UNITY_EDITOR_WIN
206178 }
207179
@@ -256,7 +228,7 @@ private static void DictationRecognizer_DictationHypothesis(string text)
256228 // We don't want to append to textSoFar yet, because the hypothesis may have changed on the next event.
257229 dictationResult = $ "{ textSoFar } { text } ...";
258230
259- InputSystem . RaiseDictationHypothesis ( source , HypothesisAction , dictationResult ) ;
231+ InputSystem . RaiseDictationHypothesis ( source , dictationResult ) ;
260232 }
261233
262234 /// <summary>
@@ -270,7 +242,7 @@ private static void DictationRecognizer_DictationResult(string text, ConfidenceL
270242
271243 dictationResult = textSoFar . ToString ( ) ;
272244
273- InputSystem . RaiseDictationResult ( source , ResultAction , dictationResult ) ;
245+ InputSystem . RaiseDictationResult ( source , dictationResult ) ;
274246 }
275247
276248 /// <summary>
@@ -288,7 +260,7 @@ private static void DictationRecognizer_DictationComplete(DictationCompletionCau
288260 dictationResult = "Dictation has timed out. Please try again." ;
289261 }
290262
291- InputSystem . RaiseDictationComplete ( source , CompleteAction , dictationResult , dictationAudioClip ) ;
263+ InputSystem . RaiseDictationComplete ( source , dictationResult , dictationAudioClip ) ;
292264 textSoFar = null ;
293265 dictationResult = string . Empty ;
294266 }
@@ -302,7 +274,7 @@ private static void DictationRecognizer_DictationError(string error, int hresult
302274 {
303275 dictationResult = $ "{ error } \n HRESULT: { hresult } ";
304276
305- InputSystem . RaiseDictationError ( source , ErrorAction , dictationResult ) ;
277+ InputSystem . RaiseDictationError ( source , dictationResult ) ;
306278 textSoFar = null ;
307279 dictationResult = string . Empty ;
308280 }
0 commit comments