11// Copyright (c) Microsoft Corporation. All rights reserved.
22// Licensed under the MIT License. See LICENSE in the project root for license information.
33
4- using System ;
54using System . Collections ;
65using System . Text ;
76using UnityEngine ;
@@ -15,82 +14,6 @@ namespace HoloToolkit.Unity.InputModule
1514 /// </summary>
1615 public class DictationInputManager : Singleton < DictationInputManager > , IInputSource
1716 {
18- /// <summary>
19- /// Initial value for InitialSilenceTimeout.
20- /// </summary>
21- private static float initialSilenceTimeout = 5f ;
22-
23- /// <summary>
24- /// The time length in seconds before dictation recognizer session ends due to lack of audio input in case there was no audio heard in the current session.
25- /// </summary>
26- public static float InitialSilenceTimeout
27- {
28- get
29- {
30- return dictationRecognizer != null ? dictationRecognizer . InitialSilenceTimeoutSeconds : initialSilenceTimeout ;
31- }
32- set
33- {
34- if ( value <= 0 ) throw new ArgumentOutOfRangeException ( "value" ) ;
35-
36- initialSilenceTimeout = value ;
37-
38- if ( dictationRecognizer != null )
39- {
40- dictationRecognizer . InitialSilenceTimeoutSeconds = initialSilenceTimeout ;
41- }
42- }
43- }
44-
45- /// <summary>
46- /// Initial value for AutoSilenceTimeout.
47- /// </summary>
48- private static float autoSilenceTimeout = 20f ;
49-
50- /// <summary>
51- /// The time length in seconds before dictation recognizer session ends due to lack of audio input.
52- /// </summary>
53- public static float AutoSilenceTimeout
54- {
55- get
56- {
57- return dictationRecognizer != null ? dictationRecognizer . AutoSilenceTimeoutSeconds : autoSilenceTimeout ;
58- }
59- set
60- {
61- if ( value <= 0 ) throw new ArgumentOutOfRangeException ( "value" ) ;
62-
63- autoSilenceTimeout = value ;
64-
65- if ( dictationRecognizer != null )
66- {
67- dictationRecognizer . AutoSilenceTimeoutSeconds = autoSilenceTimeout ;
68- }
69- }
70- }
71-
72- /// <summary>
73- /// Initial value for RecordingTime.
74- /// </summary>
75- private static int recordingTime = 10 ;
76-
77- /// <summary>
78- /// Length in seconds for the manager to listen.
79- /// </summary>
80- public static int RecordingTime
81- {
82- get
83- {
84- return recordingTime ;
85- }
86- set
87- {
88- if ( value <= 0 ) throw new ArgumentOutOfRangeException ( "value" ) ;
89-
90- recordingTime = value ;
91- }
92- }
93-
9417 /// <summary>
9518 /// Caches the text currently being displayed in dictation display text.
9619 /// </summary>
@@ -143,12 +66,9 @@ protected override void Awake()
14366 // Query the maximum frequency of the default microphone.
14467 int minSamplingRate ; // Unsued.
14568 Microphone . GetDeviceCaps ( DeviceName , out minSamplingRate , out samplingRate ) ;
146-
147- // Use this string to cache the text currently displayed.
148- textSoFar = new StringBuilder ( ) ;
14969 }
15070
151- private void Update ( )
71+ private void LateUpdate ( )
15272 {
15373 if ( IsListening && ! Microphone . IsRecording ( DeviceName ) && dictationRecognizer . Status == SpeechSystemStatus . Running )
15474 {
@@ -175,7 +95,11 @@ protected override void OnDestroy()
17595 /// <summary>
17696 /// Turns on the dictation recognizer and begins recording audio from the default microphone.
17797 /// </summary>
178- public static IEnumerator StartRecording ( )
98+ /// <param name="initialSilenceTimeout">The time length in seconds before dictation recognizer session ends due to lack of audio input in case there was no audio heard in the current session.</param>
99+ /// <param name="autoSilenceTimeout">The time length in seconds before dictation recognizer session ends due to lack of audio input.</param>
100+ /// <param name="recordingTime">Length in seconds for the manager to listen.</param>
101+ /// <returns></returns>
102+ public static IEnumerator StartRecording ( float initialSilenceTimeout = 5f , float autoSilenceTimeout = 20f , int recordingTime = 10 )
179103 {
180104 if ( IsListening ) { yield break ; }
181105
@@ -191,6 +115,8 @@ public static IEnumerator StartRecording()
191115 yield return null ;
192116 }
193117
118+ dictationRecognizer . InitialSilenceTimeoutSeconds = initialSilenceTimeout ;
119+ dictationRecognizer . AutoSilenceTimeoutSeconds = autoSilenceTimeout ;
194120 dictationRecognizer . Start ( ) ;
195121
196122 while ( dictationRecognizer . Status == SpeechSystemStatus . Failed )
@@ -205,8 +131,10 @@ public static IEnumerator StartRecording()
205131 }
206132
207133 // Start recording from the microphone.
208- dictationAudioClip = Microphone . Start ( DeviceName , false , RecordingTime , samplingRate ) ;
134+ dictationAudioClip = Microphone . Start ( DeviceName , false , recordingTime , samplingRate ) ;
209135
136+ // Use this string to cache the text currently displayed.
137+ textSoFar = new StringBuilder ( ) ;
210138 }
211139
212140 /// <summary>
@@ -277,6 +205,8 @@ private static void DictationRecognizer_DictationComplete(DictationCompletionCau
277205 }
278206
279207 InputManager . Instance . RaiseDictationComplete ( Instance , 0 , dictationResult , dictationAudioClip ) ;
208+ textSoFar = null ;
209+ dictationResult = string . Empty ;
280210 }
281211
282212 /// <summary>
@@ -289,6 +219,8 @@ private static void DictationRecognizer_DictationError(string error, int hresult
289219 dictationResult = error + "\n HRESULT: " + hresult . ToString ( ) ;
290220
291221 InputManager . Instance . RaiseDictationError ( Instance , 0 , dictationResult ) ;
222+ textSoFar = null ;
223+ dictationResult = string . Empty ;
292224 }
293225
294226 #endregion // Dictation Recognizer Callbacks
0 commit comments