Skip to content

Commit 6ab2f81

Browse files
committed
refactor from streaming example
1 parent 46289a8 commit 6ab2f81

File tree

2 files changed

+31
-31
lines changed

2 files changed

+31
-31
lines changed

Examples/ServiceExamples/Scripts/ExampleStreamingChunked.cs renamed to Examples/ServiceExamples/Scripts/ExampleStreamingSplitSamples.cs

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
using System.Collections.Generic;
2525
using UnityEngine.UI;
2626

27-
public class ExampleStreamingChunked : MonoBehaviour
27+
public class ExampleStreamingSplitSamples : MonoBehaviour
2828
{
2929
private string _username = null;
3030
private string _password = null;
@@ -37,7 +37,7 @@ public class ExampleStreamingChunked : MonoBehaviour
3737
private AudioClip _recording = null;
3838
private int _recordingBufferSize = 1;
3939
private int _recordingHZ = 22050;
40-
private int _chunkCount = 50;
40+
private int _sampleSegments = 50;
4141

4242
private SpeechToText _speechToText;
4343

@@ -105,12 +105,12 @@ private void OnError(string error)
105105
{
106106
Active = false;
107107

108-
Log.Debug("ExampleStreaming", "Error! {0}", error);
108+
Log.Debug("ExampleStreamingSplitSamples.OnError()", "Error! {0}", error);
109109
}
110110

111111
private IEnumerator RecordingHandler()
112112
{
113-
Log.Debug("ExampleStreamingChunks", "devices: {0}", Microphone.devices);
113+
Log.Debug("ExampleStreamingSplitSamples.RecordingHandler()", "devices: {0}", Microphone.devices);
114114
// Start recording
115115
_recording = Microphone.Start(_microphoneID, true, _recordingBufferSize, _recordingHZ);
116116
yield return null;
@@ -126,11 +126,11 @@ private IEnumerator RecordingHandler()
126126
DateTime now = DateTime.Now;
127127
#endif
128128

129-
// Current chunk number
130-
int chunkNum = 0;
129+
// Current sample segment number
130+
int sampleSegmentNum = 0;
131131

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;
134134

135135
// Init samples
136136
float[] samples = null;
@@ -141,61 +141,61 @@ private IEnumerator RecordingHandler()
141141
int microphonePosition = Microphone.GetPosition(_microphoneID);
142142
if (microphonePosition > _recording.samples || !Microphone.IsRecording(_microphoneID))
143143
{
144-
Log.Error("ExampleStreaming", "Microphone disconnected.");
144+
Log.Error("ExampleStreamingSplitSamples.RecordingHandler()", "Microphone disconnected.");
145145

146146
StopRecording();
147147
yield break;
148148
}
149149

150-
int sampleStart = chunkSize * chunkNum;
151-
int sampleEnd = chunkSize * (chunkNum + 1);
150+
int sampleStart = sampleSegmentSize * sampleSegmentNum;
151+
int sampleEnd = sampleSegmentSize * (sampleSegmentNum + 1);
152152

153153
#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}",
155155
microphonePosition.ToString(),
156156
sampleStart.ToString(),
157157
sampleEnd.ToString(),
158-
chunkNum.ToString());
158+
sampleSegmentNum.ToString());
159159
#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
161161
while (microphonePosition > sampleEnd || microphonePosition < sampleStart)
162162
{
163163
// 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
166166
_recording.GetData(samples, sampleStart);
167167

168168
// Create AudioData and use the samples we just created
169169
AudioData record = new AudioData();
170170
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);
172172
record.Clip.SetData(samples, 0);
173173

174174
// Send the newly created AudioData to the service
175175
_speechToText.OnListen(record);
176176

177-
// Iterate or reset chunkNum
178-
if (chunkNum < _chunkCount - 1)
177+
// Iterate or reset sampleSegmentNum
178+
if (sampleSegmentNum < _sampleSegments - 1)
179179
{
180-
chunkNum++;
180+
sampleSegmentNum++;
181181
#if ENABLE_DEBUGGING
182-
Log.Debug("ExampleStreamingChunks", "Iterating chunkNum: {0}", chunkNum);
182+
Log.Debug("ExampleStreamingSplitSamples.RecordingHandler()", "Iterating sampleSegmentNum: {0}", sampleSegmentNum);
183183
#endif
184184
}
185185
else
186186
{
187-
chunkNum = 0;
187+
sampleSegmentNum = 0;
188188
#if ENABLE_DEBUGGING
189-
Log.Debug("ExampleStreamingChunks", "Resetting chunkNum: {0}", chunkNum);
189+
Log.Debug("ExampleStreamingSplitSamples.RecordingHandler()", "Resetting sampleSegmentNum: {0}", sampleSegmentNum);
190190
#endif
191191
}
192192

193193
#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));
195195
now = DateTime.Now;
196196
#endif
197-
sampleStart = chunkSize * chunkNum;
198-
sampleEnd = chunkSize * (chunkNum + 1);
197+
sampleStart = sampleSegmentSize * sampleSegmentNum;
198+
sampleEnd = sampleSegmentSize * (sampleSegmentNum + 1);
199199
}
200200

201201
yield return 0;
@@ -213,25 +213,25 @@ private void OnRecognize(SpeechRecognitionEvent result)
213213
foreach (var alt in res.alternatives)
214214
{
215215
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);
217217
ResultsField.text = text;
218218
}
219219

220220
if (res.keywords_result != null && res.keywords_result.keyword != null)
221221
{
222222
foreach (var keyword in res.keywords_result.keyword)
223223
{
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);
225225
}
226226
}
227227

228228
if (res.word_alternatives != null)
229229
{
230230
foreach (var wordAlternative in res.word_alternatives)
231231
{
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);
233233
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);
235235
}
236236
}
237237
}
@@ -244,7 +244,7 @@ private void OnRecognizeSpeaker(SpeakerRecognitionEvent result)
244244
{
245245
foreach (SpeakerLabelsResult labelResult in result.speaker_labels)
246246
{
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));
248248
}
249249
}
250250
}

0 commit comments

Comments
 (0)