Skip to content

Commit 2c274b2

Browse files
committed
@kimberlysiva's reccomended changes
1 parent d3c0b87 commit 2c274b2

File tree

2 files changed

+25
-22
lines changed

2 files changed

+25
-22
lines changed

Examples/ServiceExamples/Scripts/ExampleStreaming.cs

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class ExampleStreaming : MonoBehaviour
3333
private string _password = null;
3434
private string _url = null;
3535
public Text ResultsField;
36-
36+
3737
private int _recordingRoutine = 0;
3838
private string _microphoneID = null;
3939
private AudioClip _recording = null;
@@ -116,7 +116,7 @@ private void OnError(string error)
116116
Log.Debug("ExampleStreaming", "Error! {0}", error);
117117
}
118118

119-
#if CHUNK_BUFFER
119+
#if CHUNK_BUFFER
120120

121121
private IEnumerator RecordingHandler()
122122
{
@@ -167,8 +167,8 @@ private IEnumerator RecordingHandler()
167167
sampleEnd.ToString(),
168168
chunkNum.ToString());
169169
#endif
170-
//If the write position is past the end of the chunk or if write position is before the start of the chunk and the chunk number is equal to the chunk count
171-
if (microphonePosition > sampleEnd || (microphonePosition < sampleStart && chunkNum == (_chunkCount - 1)))
170+
//If the write position is past the end of the chunk or if write position is before the start of the chunk
171+
while (microphonePosition > sampleEnd || microphonePosition < sampleStart)
172172
{
173173
// Init samples
174174
samples = new float[chunkSize];
@@ -204,21 +204,17 @@ private IEnumerator RecordingHandler()
204204
Log.Debug("ExampleStreamingChunks", "Sending data - time since last transmission: {0} ms", Mathf.Floor((float)(DateTime.Now - now).TotalMilliseconds));
205205
now = DateTime.Now;
206206
#endif
207+
sampleStart = chunkSize * chunkNum;
208+
sampleEnd = chunkSize * (chunkNum + 1);
207209
}
208-
else
209-
{
210-
// calculate the number of samples remaining until we ready for a block of audio,
211-
// and wait that amount of time it will take to record.
212-
int remaining = sampleEnd - microphonePosition;
213-
float timeRemaining = (float)remaining / (float)_recordingHZ;
214210

215-
yield return new WaitForSeconds(timeRemaining);
216-
}
211+
yield return 0;
217212
}
218213

219214
yield break;
220215
}
221216

217+
222218
#else
223219

224220
private IEnumerator RecordingHandler()
@@ -288,9 +284,9 @@ private void OnRecognize(SpeechRecognitionEvent result)
288284
{
289285
foreach (var alt in res.alternatives)
290286
{
291-
string text = string.Format("{0} ({1}, {2:0.00})\n", alt.transcript, res.final ? "Final" : "Interim", alt.confidence);
292-
Log.Debug("ExampleStreaming", text);
293-
ResultsField.text = text;
287+
string text = string.Format("{0} ({1}, {2:0.00})\n", alt.transcript, res.final ? "Final" : "Interim", alt.confidence);
288+
Log.Debug("ExampleStreaming", text);
289+
ResultsField.text = text;
294290
}
295291

296292
if (res.keywords_result != null && res.keywords_result.keyword != null)
@@ -306,7 +302,7 @@ private void OnRecognize(SpeechRecognitionEvent result)
306302
foreach (var wordAlternative in res.word_alternatives)
307303
{
308304
Log.Debug("ExampleSpeechToText", "Word alternatives found. Start time: {0} | EndTime: {1}", wordAlternative.start_time, wordAlternative.end_time);
309-
foreach(var alternative in wordAlternative.alternatives)
305+
foreach (var alternative in wordAlternative.alternatives)
310306
Log.Debug("ExampleSpeechToText", "\t word: {0} | confidence: {1}", alternative.word, alternative.confidence);
311307
}
312308
}

Scripts/Connection/WSConnector.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -324,13 +324,20 @@ private void SendMessages()
324324
msg = _sendQueue.Dequeue();
325325
}
326326

327-
if (msg == null)
328-
continue;
327+
while (msg != null)
328+
{
329+
if (msg is TextMessage)
330+
ws.Send(((TextMessage)msg).Text);
331+
else if (msg is BinaryMessage)
332+
ws.Send(((BinaryMessage)msg).Data);
329333

330-
if (msg is TextMessage)
331-
ws.Send(((TextMessage)msg).Text);
332-
else if (msg is BinaryMessage)
333-
ws.Send(((BinaryMessage)msg).Data);
334+
msg = null;
335+
lock (_sendQueue)
336+
{
337+
if (_sendQueue.Count > 0)
338+
msg = _sendQueue.Dequeue();
339+
}
340+
}
334341
}
335342

336343
ws.Close();

0 commit comments

Comments
 (0)