24
24
using System . Collections . Generic ;
25
25
using UnityEngine . UI ;
26
26
27
+ /// <summary>
28
+ /// This class is an example of how to stream audio from the device microphone to the Speech to Text service in Unity.
29
+ /// </summary>
27
30
public class ExampleStreamingSplitSamples : MonoBehaviour
28
31
{
29
32
private string _username = null ;
30
33
private string _password = null ;
31
34
private string _url = null ;
32
35
36
+ /// <summary>
37
+ /// Text field to display the results of streaming.
38
+ /// </summary>
33
39
public Text ResultsField ;
34
40
35
41
private int _recordingRoutine = 0 ;
36
42
private string _microphoneID = null ;
37
43
private AudioClip _recording = null ;
38
44
private int _recordingBufferSize = 1 ;
39
45
private int _recordingHZ = 22050 ;
40
- private int _sampleSegments = 50 ;
46
+ private int _sampleSegments = 50 ;
41
47
42
48
private SpeechToText _speechToText ;
43
49
@@ -54,6 +60,9 @@ void Start()
54
60
StartRecording ( ) ;
55
61
}
56
62
63
+ /// <summary>
64
+ /// Gets or sets the Active state.
65
+ /// </summary>
57
66
public bool Active
58
67
{
59
68
get { return _speechToText . IsListening ; }
@@ -105,12 +114,12 @@ private void OnError(string error)
105
114
{
106
115
Active = false ;
107
116
108
- Log . Debug ( "ExampleStreamingSplitSamples.OnError()" , "Error! {0}" , error ) ;
117
+ Log . Debug ( "ExampleStreamingSplitSamples.OnError()" , "Error! {0}" , error ) ;
109
118
}
110
119
111
120
private IEnumerator RecordingHandler ( )
112
121
{
113
- Log . Debug ( "ExampleStreamingSplitSamples.RecordingHandler()" , "devices: {0}" , Microphone . devices ) ;
122
+ Log . Debug ( "ExampleStreamingSplitSamples.RecordingHandler()" , "devices: {0}" , Microphone . devices ) ;
114
123
// Start recording
115
124
_recording = Microphone . Start ( _microphoneID , true , _recordingBufferSize , _recordingHZ ) ;
116
125
yield return null ;
@@ -127,82 +136,82 @@ private IEnumerator RecordingHandler()
127
136
#endif
128
137
129
138
// Current sample segment number
130
- int sampleSegmentNum = 0 ;
139
+ int sampleSegmentNum = 0 ;
131
140
132
141
// Size of the sample segment in samples
133
- int sampleSegmentSize = _recording . samples / _sampleSegments ;
142
+ int sampleSegmentSize = _recording . samples / _sampleSegments ;
134
143
135
144
// Init samples
136
145
float [ ] samples = null ;
137
146
138
- while ( _recordingRoutine != 0 && _recording != null )
139
- {
140
- // Get the mic position
141
- int microphonePosition = Microphone . GetPosition ( _microphoneID ) ;
142
- if ( microphonePosition > _recording . samples || ! Microphone . IsRecording ( _microphoneID ) )
143
- {
144
- Log . Error ( "ExampleStreamingSplitSamples.RecordingHandler()" , "Microphone disconnected." ) ;
145
-
146
- StopRecording ( ) ;
147
- yield break ;
148
- }
149
-
150
- int sampleStart = sampleSegmentSize * sampleSegmentNum ;
151
- int sampleEnd = sampleSegmentSize * ( sampleSegmentNum + 1 ) ;
152
-
153
- #if ENABLE_DEBUGGING
154
- Log . Debug ( "ExampleStreamingSplitSamples.RecordinHandler" , "microphonePosition: {0} | sampleStart: {1} | sampleEnd: {2} | sampleSegmentNum: {3}" ,
155
- microphonePosition . ToString ( ) ,
156
- sampleStart . ToString ( ) ,
157
- sampleEnd . ToString ( ) ,
158
- sampleSegmentNum . ToString ( ) ) ;
159
- #endif
160
- //If the write position is past the end of the sample segment or if write position is before the start of the sample segment
161
- while ( microphonePosition > sampleEnd || microphonePosition < sampleStart )
162
- {
163
- // Init samples
164
- samples = new float [ sampleSegmentSize ] ;
165
- // Write data from recording into samples starting from the sampleSegmentStart
166
- _recording . GetData ( samples , sampleStart ) ;
167
-
168
- // Create AudioData and use the samples we just created
169
- AudioData record = new AudioData ( ) ;
170
- record . MaxLevel = Mathf . Max ( Mathf . Abs ( Mathf . Min ( samples ) ) , Mathf . Max ( samples ) ) ;
171
- record . Clip = AudioClip . Create ( "Recording" , sampleSegmentSize , _recording . channels , _recordingHZ , false ) ;
172
- record . Clip . SetData ( samples , 0 ) ;
173
-
174
- // Send the newly created AudioData to the service
175
- _speechToText . OnListen ( record ) ;
176
-
177
- // Iterate or reset sampleSegmentNum
178
- if ( sampleSegmentNum < _sampleSegments - 1 )
179
- {
180
- sampleSegmentNum ++ ;
181
- #if ENABLE_DEBUGGING
182
- Log . Debug ( "ExampleStreamingSplitSamples.RecordingHandler()" , "Iterating sampleSegmentNum: {0}" , sampleSegmentNum ) ;
183
- #endif
184
- }
185
- else
186
- {
187
- sampleSegmentNum = 0 ;
188
- #if ENABLE_DEBUGGING
189
- Log . Debug ( "ExampleStreamingSplitSamples.RecordingHandler()" , "Resetting sampleSegmentNum: {0}" , sampleSegmentNum ) ;
190
- #endif
191
- }
192
-
193
- #if ENABLE_TIME_LOGGING
194
- Log . Debug ( "ExampleStreamingSplitSamples.RecordingHandler" , "Sending data - time since last transmission: {0} ms" , Mathf . Floor ( ( float ) ( DateTime . Now - now ) . TotalMilliseconds ) ) ;
195
- now = DateTime . Now ;
196
- #endif
197
- sampleStart = sampleSegmentSize * sampleSegmentNum ;
198
- sampleEnd = sampleSegmentSize * ( sampleSegmentNum + 1 ) ;
199
- }
200
-
201
- yield return 0 ;
202
- }
203
-
204
- yield break ;
205
- }
147
+ while ( _recordingRoutine != 0 && _recording != null )
148
+ {
149
+ // Get the mic position
150
+ int microphonePosition = Microphone . GetPosition ( _microphoneID ) ;
151
+ if ( microphonePosition > _recording . samples || ! Microphone . IsRecording ( _microphoneID ) )
152
+ {
153
+ Log . Error ( "ExampleStreamingSplitSamples.RecordingHandler()" , "Microphone disconnected." ) ;
154
+
155
+ StopRecording ( ) ;
156
+ yield break ;
157
+ }
158
+
159
+ int sampleStart = sampleSegmentSize * sampleSegmentNum ;
160
+ int sampleEnd = sampleSegmentSize * ( sampleSegmentNum + 1 ) ;
161
+
162
+ #if ENABLE_DEBUGGING
163
+ Log . Debug ( "ExampleStreamingSplitSamples.RecordinHandler" , "microphonePosition: {0} | sampleStart: {1} | sampleEnd: {2} | sampleSegmentNum: {3}" ,
164
+ microphonePosition . ToString ( ) ,
165
+ sampleStart . ToString ( ) ,
166
+ sampleEnd . ToString ( ) ,
167
+ sampleSegmentNum . ToString ( ) ) ;
168
+ #endif
169
+ //If the write position is past the end of the sample segment or if write position is before the start of the sample segment
170
+ while ( microphonePosition > sampleEnd || microphonePosition < sampleStart )
171
+ {
172
+ // Init samples
173
+ samples = new float [ sampleSegmentSize ] ;
174
+ // Write data from recording into samples starting from the sampleSegmentStart
175
+ _recording . GetData ( samples , sampleStart ) ;
176
+
177
+ // Create AudioData and use the samples we just created
178
+ AudioData record = new AudioData ( ) ;
179
+ record . MaxLevel = Mathf . Max ( Mathf . Abs ( Mathf . Min ( samples ) ) , Mathf . Max ( samples ) ) ;
180
+ record . Clip = AudioClip . Create ( "Recording" , sampleSegmentSize , _recording . channels , _recordingHZ , false ) ;
181
+ record . Clip . SetData ( samples , 0 ) ;
182
+
183
+ // Send the newly created AudioData to the service
184
+ _speechToText . OnListen ( record ) ;
185
+
186
+ // Iterate or reset sampleSegmentNum
187
+ if ( sampleSegmentNum < _sampleSegments - 1 )
188
+ {
189
+ sampleSegmentNum ++ ;
190
+ #if ENABLE_DEBUGGING
191
+ Log . Debug ( "ExampleStreamingSplitSamples.RecordingHandler()" , "Iterating sampleSegmentNum: {0}" , sampleSegmentNum ) ;
192
+ #endif
193
+ }
194
+ else
195
+ {
196
+ sampleSegmentNum = 0 ;
197
+ #if ENABLE_DEBUGGING
198
+ Log . Debug ( "ExampleStreamingSplitSamples.RecordingHandler()" , "Resetting sampleSegmentNum: {0}" , sampleSegmentNum ) ;
199
+ #endif
200
+ }
201
+
202
+ #if ENABLE_TIME_LOGGING
203
+ Log . Debug ( "ExampleStreamingSplitSamples.RecordingHandler" , "Sending data - time since last transmission: {0} ms" , Mathf . Floor ( ( float ) ( DateTime . Now - now ) . TotalMilliseconds ) ) ;
204
+ now = DateTime . Now ;
205
+ #endif
206
+ sampleStart = sampleSegmentSize * sampleSegmentNum ;
207
+ sampleEnd = sampleSegmentSize * ( sampleSegmentNum + 1 ) ;
208
+ }
209
+
210
+ yield return 0 ;
211
+ }
212
+
213
+ yield break ;
214
+ }
206
215
207
216
private void OnRecognize ( SpeechRecognitionEvent result )
208
217
{
@@ -213,25 +222,25 @@ private void OnRecognize(SpeechRecognitionEvent result)
213
222
foreach ( var alt in res . alternatives )
214
223
{
215
224
string text = string . Format ( "{0} ({1}, {2:0.00})\n " , alt . transcript , res . final ? "Final" : "Interim" , alt . confidence ) ;
216
- Log . Debug ( "ExampleStreamingSplitSamples.OnRecognize()" , text ) ;
225
+ Log . Debug ( "ExampleStreamingSplitSamples.OnRecognize()" , text ) ;
217
226
ResultsField . text = text ;
218
227
}
219
228
220
229
if ( res . keywords_result != null && res . keywords_result . keyword != null )
221
230
{
222
231
foreach ( var keyword in res . keywords_result . keyword )
223
232
{
224
- Log . Debug ( "ExampleStreamingSplitSamples.OnRecognize" , "keyword: {0}, confidence: {1}, start time: {2}, end time: {3}" , keyword . normalized_text , keyword . confidence , keyword . start_time , keyword . end_time ) ;
233
+ Log . Debug ( "ExampleStreamingSplitSamples.OnRecognize" , "keyword: {0}, confidence: {1}, start time: {2}, end time: {3}" , keyword . normalized_text , keyword . confidence , keyword . start_time , keyword . end_time ) ;
225
234
}
226
235
}
227
236
228
237
if ( res . word_alternatives != null )
229
238
{
230
239
foreach ( var wordAlternative in res . word_alternatives )
231
240
{
232
- Log . Debug ( "ExampleStreamingSplitSamples.OnRecognize()" , "Word alternatives found. Start time: {0} | EndTime: {1}" , wordAlternative . start_time , wordAlternative . end_time ) ;
241
+ Log . Debug ( "ExampleStreamingSplitSamples.OnRecognize()" , "Word alternatives found. Start time: {0} | EndTime: {1}" , wordAlternative . start_time , wordAlternative . end_time ) ;
233
242
foreach ( var alternative in wordAlternative . alternatives )
234
- Log . Debug ( "ExampleStreamingSplitSamples.OnRecognie()" , "\t word: {0} | confidence: {1}" , alternative . word , alternative . confidence ) ;
243
+ Log . Debug ( "ExampleStreamingSplitSamples.OnRecognie()" , "\t word: {0} | confidence: {1}" , alternative . word , alternative . confidence ) ;
235
244
}
236
245
}
237
246
}
@@ -244,7 +253,7 @@ private void OnRecognizeSpeaker(SpeakerRecognitionEvent result)
244
253
{
245
254
foreach ( SpeakerLabelsResult labelResult in result . speaker_labels )
246
255
{
247
- Log . Debug ( "ExampleStreamingSplitSamples.OnRecongizeSpeaker()" , string . Format ( "speaker result: {0} | confidence: {3} | from: {1} | to: {2}" , labelResult . speaker , labelResult . from , labelResult . to , labelResult . confidence ) ) ;
256
+ Log . Debug ( "ExampleStreamingSplitSamples.OnRecongizeSpeaker()" , string . Format ( "speaker result: {0} | confidence: {3} | from: {1} | to: {2}" , labelResult . speaker , labelResult . from , labelResult . to , labelResult . confidence ) ) ;
248
257
}
249
258
}
250
259
}
0 commit comments