4
4
using Microsoft . MixedReality . Toolkit . Internal . Definitions . InputSystem ;
5
5
using Microsoft . MixedReality . Toolkit . Internal . Interfaces . InputSystem ;
6
6
using Microsoft . MixedReality . Toolkit . Internal . Utilities . Async ;
7
+ using Microsoft . MixedReality . Toolkit . Internal . Utilities . Async . AwaitYieldInstructions ;
7
8
using System . Threading . Tasks ;
8
9
using UnityEngine ;
9
10
@@ -25,49 +26,18 @@ public class DictationInputSource : BaseGenericInputSource
25
26
/// </summary>
26
27
public static bool IsListening { get ; private set ; } = false ;
27
28
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
-
48
29
#if UNITY_STANDALONE_WIN || UNITY_WSA || UNITY_EDITOR_WIN
49
30
50
31
/// <summary>
51
32
/// Constructor.
52
33
/// </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" )
62
35
{
36
+ if ( ! Application . isPlaying ) { return ; }
37
+
63
38
source = this ;
64
39
dictationResult = string . Empty ;
65
40
66
- HypothesisAction = hypothesisAction ;
67
- ResultAction = resultAction ;
68
- CompleteAction = completeAction ;
69
- ErrorAction = errorAction ;
70
-
71
41
dictationRecognizer = new DictationRecognizer ( ) ;
72
42
dictationRecognizer . DictationHypothesis += DictationRecognizer_DictationHypothesis ;
73
43
dictationRecognizer . DictationResult += DictationRecognizer_DictationResult ;
@@ -81,16 +51,17 @@ public DictationInputSource(MixedRealityInputAction hypothesisAction,
81
51
Run ( ) ;
82
52
}
83
53
84
- /// <summary>
85
- /// Destructor.
86
- /// </summary>
87
- ~ DictationInputSource ( )
54
+ /// <inheritdoc />
55
+ public override void Dispose ( )
88
56
{
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
+ }
94
65
}
95
66
96
67
private static IMixedRealityInputSource source ;
@@ -126,7 +97,7 @@ public DictationInputSource(MixedRealityInputAction hypothesisAction,
126
97
/// </summary>
127
98
private static AudioClip dictationAudioClip ;
128
99
129
- private static readonly WaitForFixedUpdate nextUpdate = new WaitForFixedUpdate ( ) ;
100
+ private static readonly WaitForUpdate NextUpdate = new WaitForUpdate ( ) ;
130
101
131
102
private static async void Run ( )
132
103
{
@@ -141,10 +112,10 @@ private static async void Run()
141
112
if ( ! hasFailed && dictationRecognizer . Status == SpeechSystemStatus . Failed )
142
113
{
143
114
hasFailed = true ;
144
- InputSystem . RaiseDictationError ( source , ErrorAction , "Dictation recognizer has failed!" ) ;
115
+ InputSystem . RaiseDictationError ( source , "Dictation recognizer has failed!" ) ;
145
116
}
146
117
147
- await nextUpdate ;
118
+ await NextUpdate ;
148
119
}
149
120
}
150
121
@@ -192,7 +163,7 @@ public static async Task StartRecordingAsync(GameObject listener = null, float i
192
163
193
164
if ( dictationRecognizer . Status == SpeechSystemStatus . Failed )
194
165
{
195
- InputSystem . RaiseDictationError ( source , ErrorAction , "Dictation recognizer failed to start!" ) ;
166
+ InputSystem . RaiseDictationError ( source , "Dictation recognizer failed to start!" ) ;
196
167
return ;
197
168
}
198
169
@@ -201,7 +172,8 @@ public static async Task StartRecordingAsync(GameObject listener = null, float i
201
172
textSoFar = new StringBuilder ( ) ;
202
173
isTransitioning = false ;
203
174
#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." ) ;
205
177
#endif // UNITY_STANDALONE_WIN || UNITY_WSA || UNITY_EDITOR_WIN
206
178
}
207
179
@@ -256,7 +228,7 @@ private static void DictationRecognizer_DictationHypothesis(string text)
256
228
// We don't want to append to textSoFar yet, because the hypothesis may have changed on the next event.
257
229
dictationResult = $ "{ textSoFar } { text } ...";
258
230
259
- InputSystem . RaiseDictationHypothesis ( source , HypothesisAction , dictationResult ) ;
231
+ InputSystem . RaiseDictationHypothesis ( source , dictationResult ) ;
260
232
}
261
233
262
234
/// <summary>
@@ -270,7 +242,7 @@ private static void DictationRecognizer_DictationResult(string text, ConfidenceL
270
242
271
243
dictationResult = textSoFar . ToString ( ) ;
272
244
273
- InputSystem . RaiseDictationResult ( source , ResultAction , dictationResult ) ;
245
+ InputSystem . RaiseDictationResult ( source , dictationResult ) ;
274
246
}
275
247
276
248
/// <summary>
@@ -288,7 +260,7 @@ private static void DictationRecognizer_DictationComplete(DictationCompletionCau
288
260
dictationResult = "Dictation has timed out. Please try again." ;
289
261
}
290
262
291
- InputSystem . RaiseDictationComplete ( source , CompleteAction , dictationResult , dictationAudioClip ) ;
263
+ InputSystem . RaiseDictationComplete ( source , dictationResult , dictationAudioClip ) ;
292
264
textSoFar = null ;
293
265
dictationResult = string . Empty ;
294
266
}
@@ -302,7 +274,7 @@ private static void DictationRecognizer_DictationError(string error, int hresult
302
274
{
303
275
dictationResult = $ "{ error } \n HRESULT: { hresult } ";
304
276
305
- InputSystem . RaiseDictationError ( source , ErrorAction , dictationResult ) ;
277
+ InputSystem . RaiseDictationError ( source , dictationResult ) ;
306
278
textSoFar = null ;
307
279
dictationResult = string . Empty ;
308
280
}
0 commit comments