Assets/Watson/Scripts/Services/SpeechToText/SpeechToText.cs
/// <summary>
/// Gets called every time a new listening message comes in
/// </summary>
/// <param name="msg"></param>
Gets called every time a new listening message comes in
Gets called every time a new listening message comes in
private void OnListenMessage(WSConnector.Message msg)
        {
            if (msg is WSConnector.TextMessage)
            {
                WSConnector.TextMessage tm = (WSConnector.TextMessage)msg;
                IDictionary json = Json.Deserialize(tm.Text) as IDictionary;
                if (json != null)
                {
                    if (json.Contains("results"))
                    {
                        SpeechRecognitionEvent results = ParseRecognizeResponse(json);
                        if (results != null)
                        {
                            if (!EnableContinousRecognition && results.HasFinalResult()) {
                                SendStart();
                            }
                            if (m_ListenCallback != null)
                                m_ListenCallback(results);
                            else
                                StopListening();            
                        }
                        else
                            Log.Error("SpeechToText", "Failed to parse results: {0}", tm.Text);
                    }
                    else if (json.Contains("state"))
                    {
                        string state = (string)json["state"];
#if ENABLE_DEBUGGING
                        Log.Debug("SpeechToText", "Server state is {0}", state);
#endif
                        if (state == "listening")
                        {
                            if (m_IsListening)
                            {
                                if (!m_ListenActive)
                                {
                                    m_ListenActive = true;
                                    while (m_ListenRecordings.Count > 0)
                                    {
                                        AudioData clip = m_ListenRecordings.Dequeue();
                                        m_ListenSocket.Send(new WSConnector.BinaryMessage(AudioClipUtil.GetL16(clip.Clip)));
                                        m_AudioSent = true;
                                    }
                                }
                            }
                        }
                    }
                    else if (json.Contains("error"))
                    {
                        string error = (string)json["error"];
                        Log.Error("SpeechToText", "Error: {0}", error);
                        StopListening();
                        if (OnError != null)
                            OnError(error);
                    }
                    else
                    {
                        Log.Warning("SpeechToText", "Unknown message: {0}", tm.Text);
                    }
                }
                else
                {
                    Log.Error("SpeechToText", "Failed to parse JSON from server: {0}", tm.Text);
                }
            }
        }
private void OnListenMessage ( WSConnector . Message msg ) { if ( msg is WSConnector . TextMessage ) { WSConnector . TextMessage tm = ( WSConnector . TextMessage ) msg ; IDictionary json = Json . Deserialize ( tm . Text ) as IDictionary ; if ( json != null ) { if ( json . Contains ( " " ) ) { SpeechRecognitionEvent results = ParseRecognizeResponse ( json ) ; if ( results != null ) { if ( ! EnableContinousRecognition && results . HasFinalResult ( ) ) { SendStart ( ) ; } if ( m_ListenCallback != null ) m_ListenCallback ( results ) ; else StopListening ( ) ; } else Log . Error ( " " , " " , tm . Text ) ; } else if ( json . Contains ( " " ) ) { string state = ( string ) json [ " " ] ; if ENABLE_DEBUGGING Log . Debug ( " " , " " , state ) ; endif if ( state == " " ) { if ( m_IsListening ) { if ( ! m_ListenActive ) { m_ListenActive = true ; while ( m_ListenRecordings . Count > 0 ) { AudioData clip = m_ListenRecordings . Dequeue ( ) ; m_ListenSocket . Send ( new WSConnector . BinaryMessage ( AudioClipUtil . GetL16 ( clip . Clip ) ) ) ; m_AudioSent = true ; } } } } } else if ( json . Contains ( " " ) ) { string error = ( string ) json [ " " ] ; Log . Error ( " " , " " , error ) ; StopListening ( ) ; if ( OnError != null ) OnError ( error ) ; } else { Log . Warning ( " " , " " , tm . Text ) ; } } else { Log . Error ( " " , " " , tm . Text ) ; } } }
Gets called every time a new listening message comes in
Gets called every time a new listening message comes in
//UnityEngine.Debug.Log("LISTENING 1"); //UnityEngine.Debug.Log("LISTENING 2"); //UnityEngine.Debug.Log("LISTENING 3"); //UnityEngine.Debug.Log("LISTENING 4"); //RETURN HERE //results.results[0].keywords_result.keyword[0]. // when we get results, start listening for the next block .. // if continuous is true, then we don't need to do this.. //.Debug.Log("LISTENING 5"); //UnityEngine.Debug.Log("results: " + results.results[0].keywords_result.keyword[0].confidence); //UnityEngine.Debug.Log("contrec: " + !EnableContinousRecognition); //UnityEngine.Debug.Log("finalre: " + results.HasFinalResult()); // automatically stop listening if our callback is destroyed. // send all pending audio clips ..
msg
WSConnector.Message
None