Skip to content

Commit d5e572d

Browse files
committed
add @ksiva's fixes that were lost in merge
1 parent 1f3ec4e commit d5e572d

File tree

2 files changed

+120
-113
lines changed

2 files changed

+120
-113
lines changed

Examples/ServiceExamples/Scripts/ExampleStreamingChunked.cs

Lines changed: 68 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -135,74 +135,74 @@ private IEnumerator RecordingHandler()
135135
// Init samples
136136
float[] samples = null;
137137

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("ExampleStreaming", "Microphone disconnected.");
145-
146-
StopRecording();
147-
yield break;
148-
}
149-
150-
int sampleStart = chunkSize * chunkNum;
151-
int sampleEnd = chunkSize * (chunkNum + 1);
152-
153-
#if ENABLE_DEBUGGING
154-
Log.Debug("ExampleStreamingChunks", "microphonePosition: {0} | sampleStart: {1} | sampleEnd: {2} | chunkNum: {3}",
155-
microphonePosition.ToString(),
156-
sampleStart.ToString(),
157-
sampleEnd.ToString(),
158-
chunkNum.ToString());
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
161-
while (microphonePosition > sampleEnd || microphonePosition < sampleStart)
162-
{
163-
// Init samples
164-
samples = new float[chunkSize];
165-
// Write data from recording into samples starting from the chunkStart
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", chunkSize, _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 chunkNum
178-
if (chunkNum < _chunkCount - 1)
179-
{
180-
chunkNum++;
181-
#if ENABLE_DEBUGGING
182-
Log.Debug("ExampleStreamingChunks", "Iterating chunkNum: {0}", chunkNum);
183-
#endif
184-
}
185-
else
186-
{
187-
chunkNum = 0;
188-
#if ENABLE_DEBUGGING
189-
Log.Debug("ExampleStreamingChunks", "Resetting chunkNum: {0}", chunkNum);
190-
#endif
191-
}
192-
193-
#if ENABLE_TIME_LOGGING
194-
Log.Debug("ExampleStreamingChunks", "Sending data - time since last transmission: {0} ms", Mathf.Floor((float)(DateTime.Now - now).TotalMilliseconds));
195-
now = DateTime.Now;
196-
#endif
197-
sampleStart = chunkSize * chunkNum;
198-
sampleEnd = chunkSize * (chunkNum + 1);
199-
}
200-
201-
yield return 0;
202-
}
203-
204-
yield break;
205-
}
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("ExampleStreaming", "Microphone disconnected.");
145+
146+
StopRecording();
147+
yield break;
148+
}
149+
150+
int sampleStart = chunkSize * chunkNum;
151+
int sampleEnd = chunkSize * (chunkNum + 1);
152+
153+
#if ENABLE_DEBUGGING
154+
Log.Debug("ExampleStreamingChunks", "microphonePosition: {0} | sampleStart: {1} | sampleEnd: {2} | chunkNum: {3}",
155+
microphonePosition.ToString(),
156+
sampleStart.ToString(),
157+
sampleEnd.ToString(),
158+
chunkNum.ToString());
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
161+
while (microphonePosition > sampleEnd || microphonePosition < sampleStart)
162+
{
163+
// Init samples
164+
samples = new float[chunkSize];
165+
// Write data from recording into samples starting from the chunkStart
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(samples);
171+
record.Clip = AudioClip.Create("Recording", chunkSize, _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 chunkNum
178+
if (chunkNum < _chunkCount - 1)
179+
{
180+
chunkNum++;
181+
#if ENABLE_DEBUGGING
182+
Log.Debug("ExampleStreamingChunks", "Iterating chunkNum: {0}", chunkNum);
183+
#endif
184+
}
185+
else
186+
{
187+
chunkNum = 0;
188+
#if ENABLE_DEBUGGING
189+
Log.Debug("ExampleStreamingChunks", "Resetting chunkNum: {0}", chunkNum);
190+
#endif
191+
}
192+
193+
#if ENABLE_TIME_LOGGING
194+
Log.Debug("ExampleStreamingChunks", "Sending data - time since last transmission: {0} ms", Mathf.Floor((float)(DateTime.Now - now).TotalMilliseconds));
195+
now = DateTime.Now;
196+
#endif
197+
sampleStart = chunkSize * chunkNum;
198+
sampleEnd = chunkSize * (chunkNum + 1);
199+
}
200+
201+
yield return 0;
202+
}
203+
204+
yield break;
205+
}
206206

207207
private void OnRecognize(SpeechRecognitionEvent result)
208208
{

Scripts/Connection/WSConnector.cs

Lines changed: 52 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -296,51 +296,58 @@ private IEnumerator ProcessReceiveQueue()
296296
#region Threaded Functions
297297
// NOTE: ALl functions in this region are operating in a background thread, do NOT call any Unity functions!
298298
#if !NETFX_CORE
299-
private void SendMessages()
300-
{
301-
try
302-
{
303-
WebSocket ws = null;
304-
305-
ws = new WebSocket(URL);
306-
//if (Headers != null)
307-
// ws.Headers = Headers;
308-
if (Authentication != null)
309-
ws.SetCredentials(Authentication.Username, Authentication.Password, true);
310-
ws.OnOpen += OnWSOpen;
311-
ws.OnClose += OnWSClose;
312-
ws.OnError += OnWSError;
313-
ws.OnMessage += OnWSMessage;
314-
ws.Connect();
315-
316-
while (_connectionState == ConnectionState.CONNECTED)
317-
{
318-
_sendEvent.WaitOne(50);
319-
320-
Message msg = null;
321-
lock (_sendQueue)
322-
{
323-
if (_sendQueue.Count > 0)
324-
msg = _sendQueue.Dequeue();
325-
}
326-
327-
if (msg == null)
328-
continue;
329-
330-
if (msg is TextMessage)
331-
ws.Send(((TextMessage)msg).Text);
332-
else if (msg is BinaryMessage)
333-
ws.Send(((BinaryMessage)msg).Data);
334-
}
335-
336-
ws.Close();
337-
}
338-
catch (System.Exception e)
339-
{
340-
_connectionState = ConnectionState.DISCONNECTED;
341-
Log.Error("WSConnector.SendMessages()", "Caught WebSocket exception: {0}", e.ToString());
342-
}
343-
}
299+
private void SendMessages()
300+
{
301+
try
302+
{
303+
WebSocket ws = null;
304+
305+
ws = new WebSocket(URL);
306+
//if (Headers != null)
307+
// ws.Headers = Headers;
308+
if (Authentication != null)
309+
ws.SetCredentials(Authentication.Username, Authentication.Password, true);
310+
ws.OnOpen += OnWSOpen;
311+
ws.OnClose += OnWSClose;
312+
ws.OnError += OnWSError;
313+
ws.OnMessage += OnWSMessage;
314+
ws.Connect();
315+
316+
while (_connectionState == ConnectionState.CONNECTED)
317+
{
318+
_sendEvent.WaitOne(500);
319+
320+
Message msg = null;
321+
lock (_sendQueue)
322+
{
323+
if (_sendQueue.Count > 0)
324+
msg = _sendQueue.Dequeue();
325+
}
326+
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);
333+
334+
msg = null;
335+
lock (_sendQueue)
336+
{
337+
if (_sendQueue.Count > 0)
338+
msg = _sendQueue.Dequeue();
339+
}
340+
}
341+
}
342+
343+
ws.Close();
344+
}
345+
catch (System.Exception e)
346+
{
347+
_connectionState = ConnectionState.DISCONNECTED;
348+
Log.Error("WSConnector", "Caught WebSocket exception: {0}", e.ToString());
349+
}
350+
}
344351

345352
private void OnWSOpen(object sender, System.EventArgs e)
346353
{

0 commit comments

Comments
 (0)