/// <summary> /// Extract the speech-to-text result info from the next response JSON in the queue. /// </summary> void ProcessNextResponseJSON() { // Create a JSON object from the next string in the queue and process the speech-to-text result. var responseJSON = new JSONObject(m_ResponseJSONsQueue.Dequeue(), int.MaxValue); SmartLogger.Log(DebugFlags.GoogleStreamingSpeechToText, responseJSON.ToString()); string errorText = GoogleSpeechToTextResponseJSONParser.GetErrorFromResponseJSON(responseJSON); if (errorText != null) { if (m_OnError != null) { m_OnError(errorText); } } JSONObject resultsJSON = responseJSON.GetField(Constants.GoogleResponseJSONResultsFieldKey); if (resultsJSON != null && resultsJSON.Count > 0) { JSONObject resultJSON = resultsJSON[0]; SpeechToTextResult textResult = GoogleSpeechToTextResponseJSONParser.GetTextResultFromResultJSON(resultJSON); bool isFinal = false; resultJSON.GetField(out isFinal, Constants.GoogleResponseJSONResultIsFinalFieldKey, isFinal); textResult.IsFinal = isFinal; SmartLogger.Log(DebugFlags.GoogleStreamingSpeechToText, "processing result - isFinal = " + isFinal); if (m_OnTextResult != null) { m_OnTextResult(textResult); } m_LastResult = textResult; } }
/// <summary> /// Returns a speech-to-text result object based on information in the result JSON. /// </summary> /// <param name="resultJSON">Google speech-to-text result JSON object</param> /// <returns>Speech-to-text result object</returns> public static SpeechToTextResult GetTextResultFromResultJSON(JSONObject resultJSON) { SpeechToTextResult textResult = null; JSONObject alternatives = resultJSON.GetField(Constants.GoogleResponseJSONAlternativesFieldKey); if (alternatives != null) { textResult = new SpeechToTextResult(); textResult.TextAlternatives = new TextAlternative[alternatives.Count]; for (int i = 0; i < textResult.TextAlternatives.Length; ++i) { var alternative = new GoogleTextAlternative(); string text = ""; float confidence = 0; alternatives[i].GetField(out text, Constants.GoogleResponseJSONAlternativeTranscriptFieldKey, text); alternatives[i].GetField(out confidence, Constants.GoogleResponseJSONAlternativeConfidenceFieldKey, confidence); alternative.Text = text; alternative.Confidence = confidence; textResult.TextAlternatives[i] = alternative; } } if (textResult == null || textResult.TextAlternatives == null || textResult.TextAlternatives.Length == 0) { textResult = GetDefaultGoogleSpeechToTextResult(); } return(textResult); }
/// <summary> /// Function that is called when an interim text result is received. /// </summary> /// <param name="text">The interim text result</param> void OnDictationHypothesis(string text) { SmartLogger.LogFormat(DebugFlags.WindowsSpeechToText, "Dictation hypothesis: {0}", text); m_LastResult = new SpeechToTextResult(text, false); if (m_OnTextResult != null) { m_OnTextResult(m_LastResult); } }
/// <summary> /// Function that is called when a final text result is received. /// </summary> /// <param name="text">The final text result</param> /// <param name="confidence">Confidence level of the text result</param> void OnDictationResult(string text, ConfidenceLevel confidence) { SmartLogger.LogFormat(DebugFlags.WindowsSpeechToText, "Dictation result: {0}", text); m_LastResult = new SpeechToTextResult(text, true); if (m_OnTextResult != null) { m_OnTextResult(m_LastResult); } }
/// <summary> /// Starts recording audio if the service is not already recording. /// </summary> /// <returns>Whether the service successfully started recording</returns> public override bool StartRecording() { if (base.StartRecording()) { m_LastResult = new SpeechToTextResult("", false); m_DictationRecognizer.Start(); return(true); } return(false); }
/// <summary> /// Starts recording audio if the service is not already recording. /// </summary> /// <returns>Whether the service successfully started recording</returns> public override bool StartRecording() { if (base.StartRecording()) { m_LastResult = new SpeechToTextResult("", false); StartCoroutine(RecordAudio()); StartCoroutine(StreamAudioAndListenForResponses()); return(true); } return(false); }
/// <summary> /// Returns a speech-to-text result with a single empty Google text alternative. /// </summary> /// <returns>Default Google speech-to-text result object</returns> public static SpeechToTextResult GetDefaultGoogleSpeechToTextResult() { var textResult = new SpeechToTextResult(); textResult.TextAlternatives = new TextAlternative[1]; var alternative = new GoogleTextAlternative(); alternative.Text = ""; alternative.Confidence = 0; textResult.TextAlternatives[0] = alternative; return(textResult); }
/// <summary> /// Populates and returns a SpeechToTextResult object from a given Watson SpeechResult object. /// </summary> /// <param name="watsonResult">Watson SpeechResult object</param> /// <returns>A SpeechToTextResult object</returns> public SpeechToTextResult CreateSpeechToTextResult(SpeechResult watsonResult) { var textResult = new SpeechToTextResult(); textResult.IsFinal = watsonResult.Final; textResult.TextAlternatives = new TextAlternative[watsonResult.Alternatives.Length]; for (int i = 0; i < textResult.TextAlternatives.Length; ++i) { SpeechAlt watsonAlternative = watsonResult.Alternatives[i]; var alternative = new WatsonTextAlternative(); alternative.Text = watsonAlternative.Transcript; alternative.Confidence = (float)watsonAlternative.Confidence; alternative.TimeStamps = watsonAlternative.Timestamps; alternative.WordConfidenceValues = watsonAlternative.WordConfidence; textResult.TextAlternatives[i] = alternative; } return(textResult); }