Esempio n. 1
0
 void OnTextResult(SpeechToTextResult result)
 {
     TextAlternative[] a = result.TextAlternatives;
     textResult = "";
     for (int i = 0; i < a.Length; i++)
     {
         if (i > 0)
         {
             textResult += "/";
         }
         textResult += result.TextAlternatives[i].Text;
         Debug.Log(i + ":" + result.TextAlternatives[i].Text);
     }
 }
Esempio n. 2
0
        /// <summary>
        /// Translates speech to text by making a request to the speech-to-text API.
        /// </summary>
        protected override IEnumerator TranslateRecordingToText(AudioClip clip)
        {
            var request = new Request("POST", API_URL +
                                      "?" + API_PARAM_APIKEY + "=" + m_APIKey);

            request.headers.Add("Content-Type", "application/json");

            // Construct JSON request body.
            var reqJSON  = new GoogleJSONRequest();
            var reqConf  = new GoogleJSONRecognitionConfig();
            var reqAudio = new GoogleJSONRecognitionAudio();

            reqConf.encoding        = "LINEAR16";
            reqConf.sampleRateHertz = clip.frequency;
            reqConf.languageCode    = languageCode;
            reqConf.maxAlternatives = maxAlternatives;

            reqAudio.content = Convert.ToBase64String(ConvertToLinear16(clip));
            reqJSON.config   = reqConf;
            reqJSON.audio    = reqAudio;

            request.Text = JsonUtility.ToJson(reqJSON);
            request.Send();
            Debug.Log("GooglePreRecordedSpeechToTextService: Sent request");

            while (!request.isDone)
            {
                yield return(null);
            }

            // Response.
            string responseText = request.response.Text;

            if (responseText == null)
            {
                responseText = "";
            }

            // Parse error response.
            string errorMessage = null;

            try {
                GoogleJSONResponseError ers = JsonUtility.FromJson <GoogleJSONResponseError>(responseText);
                if (ers != null && ers.error != null && ers.error.code != 0)
                {
                    errorMessage = "(" + ers.error.code + ")"
                                   + (string.IsNullOrEmpty(ers.error.message) ? "unknown error" :  ers.error.message);
                }
                else if (request.response.status >= 400)
                {
                    errorMessage = "(" + request.response.status + ")Network or Http error";
                }
            } catch (ArgumentException ae) {
                errorMessage = "(" + request.response.status + ")" + ae.Message;
            }

            if (errorMessage != null)
            {
                Debug.Log("GooglePreRecordedSpeechToTextService: Error returned." + errorMessage);
                if (OnError != null)
                {
                    OnError(errorMessage);
                }
                yield break;
            }

            // Parse response.
            GoogleJSONResponse resp       = JsonUtility.FromJson <GoogleJSONResponse>(responseText);
            SpeechToTextResult textResult = new SpeechToTextResult();

            if (resp != null && resp.results != null && resp.results.Length > 0)
            {
                var resp1 = resp.results[0];
                if (resp1 != null && resp1.alternatives.Length > 0)
                {
                    textResult.TextAlternatives = resp1.alternatives;
                }
            }

            if (textResult.TextAlternatives == null || textResult.TextAlternatives.Length == 0)
            {
                textResult.TextAlternatives = new TextAlternative[1];
                TextAlternative textAlt = new GoogleJSONRecognitionAlternative();
                textAlt.Text = "";
                textResult.TextAlternatives[0] = textAlt;
            }

            Debug.Log("GooglePreRecordedSpeechToTextService: Response returned.");
            if (OnTextResult != null)
            {
                OnTextResult(textResult);
            }
        }