private void GetResponseCallback(IAsyncResult asynchronousResult)
    {
        HttpWebRequest  request     = (HttpWebRequest)asynchronousResult.AsyncState;
        HttpWebResponse response    = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
        string          finalResult = ""; // To store the final result

        using (var streamReader = new StreamReader(response.GetResponseStream()))
        {
            SpeechToTextResult speechToTextResult = JsonUtility.FromJson <SpeechToTextResult>(streamReader.ReadToEnd()); // Convert the JSON to the object

            float maxConfidence = 0.0f;                                                                                  // To check whether the result has max confidence

            if (speechToTextResult.results.Count != 0)                                                                   // If the result is not null
            {
                foreach (Alternative result in speechToTextResult.results.ElementAt(0).alternatives)                     // Iterate each results and to get the result that has max confidence
                {
                    if (maxConfidence < result.confidence)
                    {
                        finalResult   = result.transcript; // Set the new transcript that currently has max confidence level
                        maxConfidence = result.confidence; // Update the confidence with the max
                    }
                }
            }
        }

        this.result = finalResult;
        this.mutex  = true;

        // Release the HttpWebResponse
        response.Close();
    }
Exemple #2
0
 void OnTextResult(SpeechToTextResult result)
 {
     if (result.IsFinal)
     {
         Debug.Log("Final result:");
         for (int i = 0; i < result.TextAlternatives.Length; ++i)
         {
             StartCoroutine(sceneManager.instance.displayTextOnController("Voice input detected: " + result.TextAlternatives [i].Text));
             Debug.Log("Alternative " + i + ": " + result.TextAlternatives[i].Text);
             if (result.TextAlternatives [i].Text == "restart")
             {
                 gameManager.restartLevel();
             }
             else if (result.TextAlternatives [i].Text == "laser pointer" || result.TextAlternatives [i].Text == "laser")
             {
                 laserEnabled = !laserEnabled;
             }
             else if (result.TextAlternatives [i].Text == "move forward" || result.TextAlternatives [i].Text == "forward" || result.TextAlternatives [i].Text == "move")
             {
                 processLookMovement();
             }
         }
     }
     else
     {
         Debug.Log("Interim result:");
     }
 }
 /// <summary>
 /// Function that is called when a speech-to-text result is received. If it is a final result and this widget
 /// is waiting for the last result of the session, then the widget will begin processing the end results
 /// of the session.
 /// </summary>
 /// <param name="result">The speech-to-text result</param>
 void OnTextResult(SpeechToTextResult result)
 {
     if (m_WillDisplayReceivedResults)
     {
         // For the purposes of comparing results, this just uses the first alternative
         m_LastResultWasFinal = result.IsFinal;
         if (result.IsFinal)
         {
             m_PreviousFinalResults += result.TextAlternatives[0].Text;
             m_ResultsTextUI.color   = m_FinalTextResultColor;
             m_ResultsTextUI.text    = m_PreviousFinalResults;
             SmartLogger.Log(DebugFlags.SpeechToTextWidgets, m_SpeechToTextService.GetType().ToString() + " final result");
             if (m_WaitingForLastFinalResultOfSession)
             {
                 m_WaitingForLastFinalResultOfSession = false;
                 ProcessEndResults();
             }
         }
         else
         {
             m_ResultsTextUI.color = m_InterimTextResultColor;
             m_ResultsTextUI.text  = m_PreviousFinalResults + result.TextAlternatives[0].Text;
         }
     }
 }
    /// <summary>
    /// Function that is called when a speech-to-text result is received.
    /// </summary>
    /// <param name="result">The speech-to-text result</param>
    void OnTextResult(SpeechToTextResult result)
    {
        Debug.Log("OnTextResult: " + result.TextAlternatives[0].Text);

        // this just uses the first alternative
        m_LastResultWasFinal = result.IsFinal;
        if (result.IsFinal)
        {
            m_PreviousFinalResults += result.TextAlternatives[0].Text;
            m_ResultsText = m_PreviousFinalResults;
            Debug.Log(m_SpeechToTextService.GetType().ToString() + " final result");
            if (m_WaitingForLastFinalResultOfSession)
            {
                m_WaitingForLastFinalResultOfSession = false;
                ProcessEndResults();
            }
        }
    }
Exemple #5
0
 /**
  * Callbacks
  */
 //Speech to text success
 void OnTextResult(SpeechToTextResult result)
 {
     Debug.Log("Text: " + result.TextAlternatives[0].Text);
     processResult(result.TextAlternatives [0].Text);
 }