24
24
using System . Collections . Generic ;
25
25
using UnityEngine . UI ;
26
26
27
- public class ExampleStreamingChunked : MonoBehaviour
27
+ public class ExampleStreamingSplitSamples : MonoBehaviour
28
28
{
29
29
private string _username = null ;
30
30
private string _password = null ;
@@ -37,7 +37,7 @@ public class ExampleStreamingChunked : MonoBehaviour
37
37
private AudioClip _recording = null ;
38
38
private int _recordingBufferSize = 1 ;
39
39
private int _recordingHZ = 22050 ;
40
- private int _chunkCount = 50 ;
40
+ private int _sampleSegments = 50 ;
41
41
42
42
private SpeechToText _speechToText ;
43
43
@@ -105,12 +105,12 @@ private void OnError(string error)
105
105
{
106
106
Active = false ;
107
107
108
- Log . Debug ( "ExampleStreaming " , "Error! {0}" , error ) ;
108
+ Log . Debug ( "ExampleStreamingSplitSamples.OnError() " , "Error! {0}" , error ) ;
109
109
}
110
110
111
111
private IEnumerator RecordingHandler ( )
112
112
{
113
- Log . Debug ( "ExampleStreamingChunks " , "devices: {0}" , Microphone . devices ) ;
113
+ Log . Debug ( "ExampleStreamingSplitSamples.RecordingHandler() " , "devices: {0}" , Microphone . devices ) ;
114
114
// Start recording
115
115
_recording = Microphone . Start ( _microphoneID , true , _recordingBufferSize , _recordingHZ ) ;
116
116
yield return null ;
@@ -126,11 +126,11 @@ private IEnumerator RecordingHandler()
126
126
DateTime now = DateTime . Now ;
127
127
#endif
128
128
129
- // Current chunk number
130
- int chunkNum = 0 ;
129
+ // Current sample segment number
130
+ int sampleSegmentNum = 0 ;
131
131
132
- // Size of the chunk in samples
133
- int chunkSize = _recording . samples / _chunkCount ;
132
+ // Size of the sample segment in samples
133
+ int sampleSegmentSize = _recording . samples / _sampleSegments ;
134
134
135
135
// Init samples
136
136
float [ ] samples = null ;
@@ -141,61 +141,61 @@ private IEnumerator RecordingHandler()
141
141
int microphonePosition = Microphone . GetPosition ( _microphoneID ) ;
142
142
if ( microphonePosition > _recording . samples || ! Microphone . IsRecording ( _microphoneID ) )
143
143
{
144
- Log . Error ( "ExampleStreaming " , "Microphone disconnected." ) ;
144
+ Log . Error ( "ExampleStreamingSplitSamples.RecordingHandler() " , "Microphone disconnected." ) ;
145
145
146
146
StopRecording ( ) ;
147
147
yield break ;
148
148
}
149
149
150
- int sampleStart = chunkSize * chunkNum ;
151
- int sampleEnd = chunkSize * ( chunkNum + 1 ) ;
150
+ int sampleStart = sampleSegmentSize * sampleSegmentNum ;
151
+ int sampleEnd = sampleSegmentSize * ( sampleSegmentNum + 1 ) ;
152
152
153
153
#if ENABLE_DEBUGGING
154
- Log . Debug ( "ExampleStreamingChunks " , "microphonePosition: {0} | sampleStart: {1} | sampleEnd: {2} | chunkNum : {3}" ,
154
+ Log . Debug ( "ExampleStreamingSplitSamples.RecordinHandler " , "microphonePosition: {0} | sampleStart: {1} | sampleEnd: {2} | sampleSegmentNum : {3}" ,
155
155
microphonePosition . ToString ( ) ,
156
156
sampleStart . ToString ( ) ,
157
157
sampleEnd . ToString ( ) ,
158
- chunkNum . ToString ( ) ) ;
158
+ sampleSegmentNum . ToString ( ) ) ;
159
159
#endif
160
- //If the write position is past the end of the chunk or if write position is before the start of the chunk
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
161
while ( microphonePosition > sampleEnd || microphonePosition < sampleStart )
162
162
{
163
163
// Init samples
164
- samples = new float [ chunkSize ] ;
165
- // Write data from recording into samples starting from the chunkStart
164
+ samples = new float [ sampleSegmentSize ] ;
165
+ // Write data from recording into samples starting from the sampleSegmentStart
166
166
_recording . GetData ( samples , sampleStart ) ;
167
167
168
168
// Create AudioData and use the samples we just created
169
169
AudioData record = new AudioData ( ) ;
170
170
record . MaxLevel = Mathf . Max ( Mathf . Abs ( Mathf . Min ( samples ) ) , Mathf . Max ( samples ) ) ;
171
- record . Clip = AudioClip . Create ( "Recording" , chunkSize , _recording . channels , _recordingHZ , false ) ;
171
+ record . Clip = AudioClip . Create ( "Recording" , sampleSegmentSize , _recording . channels , _recordingHZ , false ) ;
172
172
record . Clip . SetData ( samples , 0 ) ;
173
173
174
174
// Send the newly created AudioData to the service
175
175
_speechToText . OnListen ( record ) ;
176
176
177
- // Iterate or reset chunkNum
178
- if ( chunkNum < _chunkCount - 1 )
177
+ // Iterate or reset sampleSegmentNum
178
+ if ( sampleSegmentNum < _sampleSegments - 1 )
179
179
{
180
- chunkNum ++ ;
180
+ sampleSegmentNum ++ ;
181
181
#if ENABLE_DEBUGGING
182
- Log . Debug ( "ExampleStreamingChunks " , "Iterating chunkNum : {0}" , chunkNum ) ;
182
+ Log . Debug ( "ExampleStreamingSplitSamples.RecordingHandler() " , "Iterating sampleSegmentNum : {0}" , sampleSegmentNum ) ;
183
183
#endif
184
184
}
185
185
else
186
186
{
187
- chunkNum = 0 ;
187
+ sampleSegmentNum = 0 ;
188
188
#if ENABLE_DEBUGGING
189
- Log . Debug ( "ExampleStreamingChunks " , "Resetting chunkNum : {0}" , chunkNum ) ;
189
+ Log . Debug ( "ExampleStreamingSplitSamples.RecordingHandler() " , "Resetting sampleSegmentNum : {0}" , sampleSegmentNum ) ;
190
190
#endif
191
191
}
192
192
193
193
#if ENABLE_TIME_LOGGING
194
- Log . Debug ( "ExampleStreamingChunks " , "Sending data - time since last transmission: {0} ms" , Mathf . Floor ( ( float ) ( DateTime . Now - now ) . TotalMilliseconds ) ) ;
194
+ Log . Debug ( "ExampleStreamingSplitSamples.RecordingHandler " , "Sending data - time since last transmission: {0} ms" , Mathf . Floor ( ( float ) ( DateTime . Now - now ) . TotalMilliseconds ) ) ;
195
195
now = DateTime . Now ;
196
196
#endif
197
- sampleStart = chunkSize * chunkNum ;
198
- sampleEnd = chunkSize * ( chunkNum + 1 ) ;
197
+ sampleStart = sampleSegmentSize * sampleSegmentNum ;
198
+ sampleEnd = sampleSegmentSize * ( sampleSegmentNum + 1 ) ;
199
199
}
200
200
201
201
yield return 0 ;
@@ -213,25 +213,25 @@ private void OnRecognize(SpeechRecognitionEvent result)
213
213
foreach ( var alt in res . alternatives )
214
214
{
215
215
string text = string . Format ( "{0} ({1}, {2:0.00})\n " , alt . transcript , res . final ? "Final" : "Interim" , alt . confidence ) ;
216
- Log . Debug ( "ExampleStreaming " , text ) ;
216
+ Log . Debug ( "ExampleStreamingSplitSamples.OnRecognize() " , text ) ;
217
217
ResultsField . text = text ;
218
218
}
219
219
220
220
if ( res . keywords_result != null && res . keywords_result . keyword != null )
221
221
{
222
222
foreach ( var keyword in res . keywords_result . keyword )
223
223
{
224
- Log . Debug ( "ExampleSpeechToText " , "keyword: {0}, confidence: {1}, start time: {2}, end time: {3}" , keyword . normalized_text , keyword . confidence , keyword . start_time , keyword . end_time ) ;
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 ) ;
225
225
}
226
226
}
227
227
228
228
if ( res . word_alternatives != null )
229
229
{
230
230
foreach ( var wordAlternative in res . word_alternatives )
231
231
{
232
- Log . Debug ( "ExampleSpeechToText " , "Word alternatives found. Start time: {0} | EndTime: {1}" , wordAlternative . start_time , wordAlternative . end_time ) ;
232
+ Log . Debug ( "ExampleStreamingSplitSamples.OnRecognize() " , "Word alternatives found. Start time: {0} | EndTime: {1}" , wordAlternative . start_time , wordAlternative . end_time ) ;
233
233
foreach ( var alternative in wordAlternative . alternatives )
234
- Log . Debug ( "ExampleSpeechToText " , "\t word: {0} | confidence: {1}" , alternative . word , alternative . confidence ) ;
234
+ Log . Debug ( "ExampleStreamingSplitSamples.OnRecognie() " , "\t word: {0} | confidence: {1}" , alternative . word , alternative . confidence ) ;
235
235
}
236
236
}
237
237
}
@@ -244,7 +244,7 @@ private void OnRecognizeSpeaker(SpeakerRecognitionEvent result)
244
244
{
245
245
foreach ( SpeakerLabelsResult labelResult in result . speaker_labels )
246
246
{
247
- Log . Debug ( "ExampleStreaming " , string . Format ( "speaker result: {0} | confidence: {3} | from: {1} | to: {2}" , labelResult . speaker , labelResult . from , labelResult . to , labelResult . confidence ) ) ;
247
+ Log . Debug ( "ExampleStreamingSplitSamples.OnRecongizeSpeaker() " , string . Format ( "speaker result: {0} | confidence: {3} | from: {1} | to: {2}" , labelResult . speaker , labelResult . from , labelResult . to , labelResult . confidence ) ) ;
248
248
}
249
249
}
250
250
}
0 commit comments