From 91d53b64031b696d899a5a6947cfa04c3dd4aa2d Mon Sep 17 00:00:00 2001 From: Ajiemar Santiago Date: Mon, 24 Oct 2016 15:10:02 -0500 Subject: [PATCH 1/2] example of stt streaming --- .../ServiceExamples/ExampleStreaming.unity | 282 ++++++++++++++++++ .../ExampleStreaming.unity.meta | 8 + .../Scripts/ExampleStreaming.cs | 168 +++++++++++ .../Scripts/ExampleStreaming.cs.meta | 12 + 4 files changed, 470 insertions(+) create mode 100755 Examples/ServiceExamples/ExampleStreaming.unity create mode 100755 Examples/ServiceExamples/ExampleStreaming.unity.meta create mode 100755 Examples/ServiceExamples/Scripts/ExampleStreaming.cs create mode 100755 Examples/ServiceExamples/Scripts/ExampleStreaming.cs.meta diff --git a/Examples/ServiceExamples/ExampleStreaming.unity b/Examples/ServiceExamples/ExampleStreaming.unity new file mode 100755 index 000000000..2d5d7cffd --- /dev/null +++ b/Examples/ServiceExamples/ExampleStreaming.unity @@ -0,0 +1,282 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!29 &1 +SceneSettings: + m_ObjectHideFlags: 0 + m_PVSData: + m_PVSObjectsArray: [] + m_PVSPortalsArray: [] + m_OcclusionBakeSettings: + smallestOccluder: 5 + smallestHole: 0.25 + backfaceThreshold: 100 +--- !u!104 &2 +RenderSettings: + m_ObjectHideFlags: 0 + serializedVersion: 7 + m_Fog: 0 + m_FogColor: {r: 0.5, g: 0.5, b: 0.5, a: 1} + m_FogMode: 3 + m_FogDensity: 0.01 + m_LinearFogStart: 0 + m_LinearFogEnd: 300 + m_AmbientSkyColor: {r: 0.212, g: 0.227, b: 0.259, a: 1} + m_AmbientEquatorColor: {r: 0.114, g: 0.125, b: 0.133, a: 1} + m_AmbientGroundColor: {r: 0.047, g: 0.043, b: 0.035, a: 1} + m_AmbientIntensity: 1 + m_AmbientMode: 0 + m_SkyboxMaterial: {fileID: 10304, guid: 0000000000000000f000000000000000, type: 0} + m_HaloStrength: 0.5 + m_FlareStrength: 1 + m_FlareFadeSpeed: 3 + m_HaloTexture: {fileID: 0} + m_SpotCookie: {fileID: 10001, guid: 0000000000000000e000000000000000, type: 0} + m_DefaultReflectionMode: 0 + m_DefaultReflectionResolution: 128 + m_ReflectionBounces: 1 + m_ReflectionIntensity: 1 + m_CustomReflection: {fileID: 0} + m_Sun: {fileID: 0} + m_IndirectSpecularColor: {r: 0.4469244, g: 0.4967847, b: 0.575083, a: 1} +--- !u!157 &3 +LightmapSettings: + m_ObjectHideFlags: 0 + serializedVersion: 7 + m_GIWorkflowMode: 0 + m_GISettings: + serializedVersion: 2 + m_BounceScale: 1 + m_IndirectOutputScale: 1 + m_AlbedoBoost: 1 + m_TemporalCoherenceThreshold: 1 + m_EnvironmentLightingMode: 0 + m_EnableBakedLightmaps: 1 + m_EnableRealtimeLightmaps: 1 + m_LightmapEditorSettings: + serializedVersion: 4 + m_Resolution: 2 + m_BakeResolution: 40 + m_TextureWidth: 1024 + m_TextureHeight: 1024 + m_AO: 0 + m_AOMaxDistance: 1 + m_CompAOExponent: 1 + m_CompAOExponentDirect: 0 + m_Padding: 2 + m_LightmapParameters: {fileID: 0} + m_LightmapsBakeMode: 1 + m_TextureCompression: 1 + m_DirectLightInLightProbes: 1 + m_FinalGather: 0 + m_FinalGatherFiltering: 1 + m_FinalGatherRayCount: 256 + m_ReflectionCompression: 2 + m_LightingDataAsset: {fileID: 0} + m_RuntimeCPUUsage: 25 +--- !u!196 &4 +NavMeshSettings: + serializedVersion: 2 + m_ObjectHideFlags: 0 + m_BuildSettings: + serializedVersion: 2 + agentRadius: 0.5 + agentHeight: 2 + agentSlope: 45 + agentClimb: 0.4 + ledgeDropHeight: 0 + maxJumpAcrossDistance: 0 + accuratePlacement: 0 + minRegionArea: 2 + cellSize: 0.16666667 + manualCellSize: 0 + m_NavMeshData: {fileID: 0} +--- !u!1 &1159842907 +GameObject: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + serializedVersion: 4 + m_Component: + - 4: {fileID: 1159842909} + - 108: {fileID: 1159842908} + m_Layer: 0 + m_Name: Directional Light + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!108 &1159842908 +Light: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 1159842907} + m_Enabled: 1 + serializedVersion: 7 + m_Type: 1 + m_Color: {r: 1, g: 0.95686275, b: 0.8392157, a: 1} + m_Intensity: 1 + m_Range: 10 + m_SpotAngle: 30 + m_CookieSize: 10 + m_Shadows: + m_Type: 2 + m_Resolution: -1 + m_CustomResolution: -1 + m_Strength: 1 + m_Bias: 0.05 + m_NormalBias: 0.4 + m_NearPlane: 0.2 + m_Cookie: {fileID: 0} + m_DrawHalo: 0 + m_Flare: {fileID: 0} + m_RenderMode: 0 + m_CullingMask: + serializedVersion: 2 + m_Bits: 4294967295 + m_Lightmapping: 4 + m_AreaSize: {x: 1, y: 1} + m_BounceIntensity: 1 + m_ShadowRadius: 0 + m_ShadowAngle: 0 +--- !u!4 &1159842909 +Transform: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 1159842907} + m_LocalRotation: {x: 0.40821788, y: -0.23456968, z: 0.10938163, w: 0.8754261} + m_LocalPosition: {x: 0, y: 3, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_LocalEulerAnglesHint: {x: 50, y: -30, z: 0} + m_Children: [] + m_Father: {fileID: 0} + m_RootOrder: 1 +--- !u!1 &1391086061 +GameObject: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + serializedVersion: 4 + m_Component: + - 4: {fileID: 1391086066} + - 20: {fileID: 1391086065} + - 92: {fileID: 1391086064} + - 124: {fileID: 1391086063} + - 81: {fileID: 1391086062} + m_Layer: 0 + m_Name: Main Camera + m_TagString: MainCamera + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!81 &1391086062 +AudioListener: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 1391086061} + m_Enabled: 1 +--- !u!124 &1391086063 +Behaviour: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 1391086061} + m_Enabled: 1 +--- !u!92 &1391086064 +Behaviour: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 1391086061} + m_Enabled: 1 +--- !u!20 &1391086065 +Camera: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 1391086061} + m_Enabled: 1 + serializedVersion: 2 + m_ClearFlags: 1 + m_BackGroundColor: {r: 0.19215687, g: 0.3019608, b: 0.4745098, a: 0} + m_NormalizedViewPortRect: + serializedVersion: 2 + x: 0 + y: 0 + width: 1 + height: 1 + near clip plane: 0.3 + far clip plane: 1000 + field of view: 60 + orthographic: 0 + orthographic size: 5 + m_Depth: -1 + m_CullingMask: + serializedVersion: 2 + m_Bits: 4294967295 + m_RenderingPath: -1 + m_TargetTexture: {fileID: 0} + m_TargetDisplay: 0 + m_TargetEye: 3 + m_HDR: 0 + m_OcclusionCulling: 1 + m_StereoConvergence: 10 + m_StereoSeparation: 0.022 + m_StereoMirrorMode: 0 +--- !u!4 &1391086066 +Transform: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 1391086061} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 1, z: -10} + m_LocalScale: {x: 1, y: 1, z: 1} + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} + m_Children: [] + m_Father: {fileID: 0} + m_RootOrder: 0 +--- !u!1 &1646685149 +GameObject: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + serializedVersion: 4 + m_Component: + - 4: {fileID: 1646685151} + - 114: {fileID: 1646685150} + m_Layer: 0 + m_Name: ExampleStreaming + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!114 &1646685150 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 1646685149} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 6c5299c1c838a8c44b49ecc9254704e0, type: 3} + m_Name: + m_EditorClassIdentifier: +--- !u!4 &1646685151 +Transform: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 1646685149} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} + m_Children: [] + m_Father: {fileID: 0} + m_RootOrder: 2 diff --git a/Examples/ServiceExamples/ExampleStreaming.unity.meta b/Examples/ServiceExamples/ExampleStreaming.unity.meta new file mode 100755 index 000000000..7873c0333 --- /dev/null +++ b/Examples/ServiceExamples/ExampleStreaming.unity.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 57c7b454bb032a34fa0f938231dba758 +timeCreated: 1477325083 +licenseType: Pro +DefaultImporter: + userData: + assetBundleName: + assetBundleVariant: diff --git a/Examples/ServiceExamples/Scripts/ExampleStreaming.cs b/Examples/ServiceExamples/Scripts/ExampleStreaming.cs new file mode 100755 index 000000000..4ba1e19e6 --- /dev/null +++ b/Examples/ServiceExamples/Scripts/ExampleStreaming.cs @@ -0,0 +1,168 @@ +/** +* Copyright 2015 IBM Corp. All Rights Reserved. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +* +*/ + +using UnityEngine; +using System.Collections; +using IBM.Watson.DeveloperCloud.Logging; +using IBM.Watson.DeveloperCloud.Services.SpeechToText.v1; +using IBM.Watson.DeveloperCloud.Utilities; +using IBM.Watson.DeveloperCloud.DataTypes; + +public class ExampleStreaming : MonoBehaviour +{ + private int m_RecordingRoutine = 0; + + + private string m_MicrophoneID = null; + private AudioClip m_Recording = null; + private int m_RecordingBufferSize = 2; + private int m_RecordingHZ = 22050; + + + private SpeechToText m_SpeechToText = new SpeechToText(); + + void Start() + { + LogSystem.InstallDefaultReactors(); + Log.Debug("ExampleStreaming", "Start();"); + + Active = true; + + StartRecording(); + } + + public bool Active + { + get { return m_SpeechToText.IsListening; } + set { + if ( value && !m_SpeechToText.IsListening ) + { + m_SpeechToText.DetectSilence = true; + m_SpeechToText.EnableWordConfidence = false; + m_SpeechToText.EnableTimestamps = false; + m_SpeechToText.SilenceThreshold = 0.03f; + m_SpeechToText.MaxAlternatives = 1; + m_SpeechToText.EnableContinousRecognition = true; + m_SpeechToText.EnableInterimResults = true; + m_SpeechToText.OnError = OnError; + m_SpeechToText.StartListening( OnRecognize ); + } + else if ( !value && m_SpeechToText.IsListening ) + { + m_SpeechToText.StopListening(); + } + } + } + + private void StartRecording() + { + if (m_RecordingRoutine == 0) + { + UnityObjectUtil.StartDestroyQueue(); + m_RecordingRoutine = Runnable.Run(RecordingHandler()); + } + } + + private void StopRecording() + { + if (m_RecordingRoutine != 0) + { + Microphone.End(m_MicrophoneID); + Runnable.Stop(m_RecordingRoutine); + m_RecordingRoutine = 0; + } + } + + private void OnError( string error ) + { + Active = false; + + Log.Debug("ExampleStreaming", "Error! {0}", error); + } + + private IEnumerator RecordingHandler() + { + m_Recording = Microphone.Start(m_MicrophoneID, true, m_RecordingBufferSize, m_RecordingHZ); + yield return null; // let m_RecordingRoutine get set.. + + if (m_Recording == null) + { + StopRecording(); + yield break; + } + + bool bFirstBlock = true; + int midPoint = m_Recording.samples / 2; + float[] samples = null; + + while (m_RecordingRoutine != 0 && m_Recording != null) + { + int writePos = Microphone.GetPosition(m_MicrophoneID); + if (writePos > m_Recording.samples || !Microphone.IsRecording(m_MicrophoneID)) + { + Log.Error("MicrophoneWidget", "Microphone disconnected."); + + StopRecording(); + yield break; + } + + if ((bFirstBlock && writePos >= midPoint) + || (!bFirstBlock && writePos < midPoint)) + { + // front block is recorded, make a RecordClip and pass it onto our callback. + samples = new float[midPoint]; + m_Recording.GetData(samples, bFirstBlock ? 0 : midPoint); + + AudioData record = new AudioData(); + record.MaxLevel = Mathf.Max(samples); + record.Clip = AudioClip.Create("Recording", midPoint, m_Recording.channels, m_RecordingHZ, false); + record.Clip.SetData(samples, 0); + + m_SpeechToText.OnListen(record); + + bFirstBlock = !bFirstBlock; + } + else + { + // calculate the number of samples remaining until we ready for a block of audio, + // and wait that amount of time it will take to record. + int remaining = bFirstBlock ? (midPoint - writePos) : (m_Recording.samples - writePos); + float timeRemaining = (float)remaining / (float)m_RecordingHZ; + + yield return new WaitForSeconds(timeRemaining); + } + + } + + yield break; + } + + private void OnRecognize(SpeechRecognitionEvent result) + { + if (result != null && result.results.Length > 0) + { + foreach (var res in result.results) + { + foreach (var alt in res.alternatives) + { + string text = alt.transcript; + Log.Debug("ExampleStreaming", string.Format("{0} ({1}, {2:0.00})\n", text, res.final ? "Final" : "Interim", alt.confidence)); + } + } + } + } +} \ No newline at end of file diff --git a/Examples/ServiceExamples/Scripts/ExampleStreaming.cs.meta b/Examples/ServiceExamples/Scripts/ExampleStreaming.cs.meta new file mode 100755 index 000000000..a7a574279 --- /dev/null +++ b/Examples/ServiceExamples/Scripts/ExampleStreaming.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 6c5299c1c838a8c44b49ecc9254704e0 +timeCreated: 1477325099 +licenseType: Pro +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: From d55e742b5278347e7dee97e14eac59db6b52508c Mon Sep 17 00:00:00 2001 From: Ajiemar Santiago Date: Tue, 25 Oct 2016 17:36:21 -0500 Subject: [PATCH 2/2] formatting, trace recording device to console --- .../Scripts/ExampleStreaming.cs | 281 +++++++++--------- 1 file changed, 140 insertions(+), 141 deletions(-) diff --git a/Examples/ServiceExamples/Scripts/ExampleStreaming.cs b/Examples/ServiceExamples/Scripts/ExampleStreaming.cs index 4ba1e19e6..59ee303e9 100755 --- a/Examples/ServiceExamples/Scripts/ExampleStreaming.cs +++ b/Examples/ServiceExamples/Scripts/ExampleStreaming.cs @@ -24,145 +24,144 @@ public class ExampleStreaming : MonoBehaviour { - private int m_RecordingRoutine = 0; - - - private string m_MicrophoneID = null; - private AudioClip m_Recording = null; - private int m_RecordingBufferSize = 2; - private int m_RecordingHZ = 22050; - - - private SpeechToText m_SpeechToText = new SpeechToText(); - - void Start() - { - LogSystem.InstallDefaultReactors(); - Log.Debug("ExampleStreaming", "Start();"); - - Active = true; - - StartRecording(); - } - - public bool Active - { - get { return m_SpeechToText.IsListening; } - set { - if ( value && !m_SpeechToText.IsListening ) - { - m_SpeechToText.DetectSilence = true; - m_SpeechToText.EnableWordConfidence = false; - m_SpeechToText.EnableTimestamps = false; - m_SpeechToText.SilenceThreshold = 0.03f; - m_SpeechToText.MaxAlternatives = 1; - m_SpeechToText.EnableContinousRecognition = true; - m_SpeechToText.EnableInterimResults = true; - m_SpeechToText.OnError = OnError; - m_SpeechToText.StartListening( OnRecognize ); - } - else if ( !value && m_SpeechToText.IsListening ) - { - m_SpeechToText.StopListening(); - } - } - } - - private void StartRecording() - { - if (m_RecordingRoutine == 0) - { - UnityObjectUtil.StartDestroyQueue(); - m_RecordingRoutine = Runnable.Run(RecordingHandler()); - } - } - - private void StopRecording() - { - if (m_RecordingRoutine != 0) - { - Microphone.End(m_MicrophoneID); - Runnable.Stop(m_RecordingRoutine); - m_RecordingRoutine = 0; - } - } - - private void OnError( string error ) - { - Active = false; - - Log.Debug("ExampleStreaming", "Error! {0}", error); - } - - private IEnumerator RecordingHandler() - { - m_Recording = Microphone.Start(m_MicrophoneID, true, m_RecordingBufferSize, m_RecordingHZ); - yield return null; // let m_RecordingRoutine get set.. - - if (m_Recording == null) - { - StopRecording(); - yield break; - } - - bool bFirstBlock = true; - int midPoint = m_Recording.samples / 2; - float[] samples = null; - - while (m_RecordingRoutine != 0 && m_Recording != null) - { - int writePos = Microphone.GetPosition(m_MicrophoneID); - if (writePos > m_Recording.samples || !Microphone.IsRecording(m_MicrophoneID)) - { - Log.Error("MicrophoneWidget", "Microphone disconnected."); - - StopRecording(); - yield break; - } - - if ((bFirstBlock && writePos >= midPoint) - || (!bFirstBlock && writePos < midPoint)) - { - // front block is recorded, make a RecordClip and pass it onto our callback. - samples = new float[midPoint]; - m_Recording.GetData(samples, bFirstBlock ? 0 : midPoint); - - AudioData record = new AudioData(); - record.MaxLevel = Mathf.Max(samples); - record.Clip = AudioClip.Create("Recording", midPoint, m_Recording.channels, m_RecordingHZ, false); - record.Clip.SetData(samples, 0); - - m_SpeechToText.OnListen(record); - - bFirstBlock = !bFirstBlock; - } - else - { - // calculate the number of samples remaining until we ready for a block of audio, - // and wait that amount of time it will take to record. - int remaining = bFirstBlock ? (midPoint - writePos) : (m_Recording.samples - writePos); - float timeRemaining = (float)remaining / (float)m_RecordingHZ; - - yield return new WaitForSeconds(timeRemaining); - } - - } - - yield break; - } - - private void OnRecognize(SpeechRecognitionEvent result) - { - if (result != null && result.results.Length > 0) - { - foreach (var res in result.results) - { - foreach (var alt in res.alternatives) - { - string text = alt.transcript; - Log.Debug("ExampleStreaming", string.Format("{0} ({1}, {2:0.00})\n", text, res.final ? "Final" : "Interim", alt.confidence)); - } - } - } - } + private int m_RecordingRoutine = 0; + private string m_MicrophoneID = null; + private AudioClip m_Recording = null; + private int m_RecordingBufferSize = 2; + private int m_RecordingHZ = 22050; + + private SpeechToText m_SpeechToText = new SpeechToText(); + + void Start() + { + LogSystem.InstallDefaultReactors(); + Log.Debug("ExampleStreaming", "Start();"); + + Active = true; + + StartRecording(); + } + + public bool Active + { + get { return m_SpeechToText.IsListening; } + set + { + if (value && !m_SpeechToText.IsListening) + { + m_SpeechToText.DetectSilence = true; + m_SpeechToText.EnableWordConfidence = false; + m_SpeechToText.EnableTimestamps = false; + m_SpeechToText.SilenceThreshold = 0.03f; + m_SpeechToText.MaxAlternatives = 1; + m_SpeechToText.EnableContinousRecognition = true; + m_SpeechToText.EnableInterimResults = true; + m_SpeechToText.OnError = OnError; + m_SpeechToText.StartListening(OnRecognize); + } + else if (!value && m_SpeechToText.IsListening) + { + m_SpeechToText.StopListening(); + } + } + } + + private void StartRecording() + { + if (m_RecordingRoutine == 0) + { + UnityObjectUtil.StartDestroyQueue(); + m_RecordingRoutine = Runnable.Run(RecordingHandler()); + } + } + + private void StopRecording() + { + if (m_RecordingRoutine != 0) + { + Microphone.End(m_MicrophoneID); + Runnable.Stop(m_RecordingRoutine); + m_RecordingRoutine = 0; + } + } + + private void OnError(string error) + { + Active = false; + + Log.Debug("ExampleStreaming", "Error! {0}", error); + } + + private IEnumerator RecordingHandler() + { + Log.Debug("ExampleStreaming", "devices: {0}", Microphone.devices); + m_Recording = Microphone.Start(m_MicrophoneID, true, m_RecordingBufferSize, m_RecordingHZ); + yield return null; // let m_RecordingRoutine get set.. + + if (m_Recording == null) + { + StopRecording(); + yield break; + } + + bool bFirstBlock = true; + int midPoint = m_Recording.samples / 2; + float[] samples = null; + + while (m_RecordingRoutine != 0 && m_Recording != null) + { + int writePos = Microphone.GetPosition(m_MicrophoneID); + if (writePos > m_Recording.samples || !Microphone.IsRecording(m_MicrophoneID)) + { + Log.Error("MicrophoneWidget", "Microphone disconnected."); + + StopRecording(); + yield break; + } + + if ((bFirstBlock && writePos >= midPoint) + || (!bFirstBlock && writePos < midPoint)) + { + // front block is recorded, make a RecordClip and pass it onto our callback. + samples = new float[midPoint]; + m_Recording.GetData(samples, bFirstBlock ? 0 : midPoint); + + AudioData record = new AudioData(); + record.MaxLevel = Mathf.Max(samples); + record.Clip = AudioClip.Create("Recording", midPoint, m_Recording.channels, m_RecordingHZ, false); + record.Clip.SetData(samples, 0); + + m_SpeechToText.OnListen(record); + + bFirstBlock = !bFirstBlock; + } + else + { + // calculate the number of samples remaining until we ready for a block of audio, + // and wait that amount of time it will take to record. + int remaining = bFirstBlock ? (midPoint - writePos) : (m_Recording.samples - writePos); + float timeRemaining = (float)remaining / (float)m_RecordingHZ; + + yield return new WaitForSeconds(timeRemaining); + } + + } + + yield break; + } + + private void OnRecognize(SpeechRecognitionEvent result) + { + if (result != null && result.results.Length > 0) + { + foreach (var res in result.results) + { + foreach (var alt in res.alternatives) + { + string text = alt.transcript; + Log.Debug("ExampleStreaming", string.Format("{0} ({1}, {2:0.00})\n", text, res.final ? "Final" : "Interim", alt.confidence)); + } + } + } + } } \ No newline at end of file