Assets/SpeechToText/Scripts/SpeechToTextServices/WitAiNonStreamingSpeechToTextService.cs
/// <summary>
/// Translates speech to text by making a request to the speech-to-text API.
/// </summary>
Translates speech to text by making a request to the speech-to-text API.
Translates speech to text by making a request to the speech - to - text API .
protected override IEnumerator TranslateRecordingToText()
        {
            m_TempAudioComponent.ClearTempAudioFiles();
            string recordedAudioFilePath = SavWav.Save(m_TempAudioComponent.TempAudioRelativePath(), AudioRecordingManager.Instance.RecordedAudio);
            var request = new Request("POST", Constants.WitAiSpeechToTextBaseURL + "?" +
                Constants.WitAiVersionParameterName + "=" + DateTime.Now.ToString(Constants.WitAiVersionDateFormat));
            request.headers.Add("Authorization", "Bearer " + m_APIAccessToken);
            request.headers.Add("Content-Type", "audio/wav");
            request.Bytes = File.ReadAllBytes(recordedAudioFilePath);
            SmartLogger.Log(DebugFlags.WitAINonStreamingSpeechToText, "Sending request");
            request.Send();
            float startTime = Time.time;
            while (!request.isDone)
            {
                yield return null;
            }
            SmartLogger.Log(DebugFlags.WitAINonStreamingSpeechToText, "response time: " + (Time.time - startTime));
            var responseJSON = new JSONObject(request.response.Text, int.MaxValue);
            SmartLogger.Log(DebugFlags.WitAINonStreamingSpeechToText, "Received request result");
            SmartLogger.Log(DebugFlags.WitAINonStreamingSpeechToText, responseJSON.ToString());
            string errorText = WitAiSpeechToTextResponseJSONParser.GetErrorFromResponseJSON(responseJSON);
            if (errorText != null)
            {
                if (m_OnError != null)
                {
                    m_OnError(errorText);
                }
            }
            if (m_OnTextResult != null)
            {
                m_OnTextResult(WitAiSpeechToTextResponseJSONParser.GetTextResultFromResponseJSON(responseJSON));
            }
            m_TempAudioComponent.ClearTempAudioFiles();
        }
protected override IEnumerator TranslateRecordingToText ( ) { m_TempAudioComponent . ClearTempAudioFiles ( ) ; string recordedAudioFilePath = SavWav . Save ( m_TempAudioComponent . TempAudioRelativePath ( ) , AudioRecordingManager . Instance . RecordedAudio ) ; var request = new Request ( " " , Constants . WitAiSpeechToTextBaseURL + " " + Constants . WitAiVersionParameterName + " " + DateTime . Now . ToString ( Constants . WitAiVersionDateFormat ) ) ; request . headers . Add ( " " , " " + m_APIAccessToken ) ; request . headers . Add ( " " , " " ) ; request . Bytes = File . ReadAllBytes ( recordedAudioFilePath ) ; SmartLogger . Log ( DebugFlags . WitAINonStreamingSpeechToText , " " ) ; request . Send ( ) ; float startTime = Time . time ; while ( ! request . isDone ) { yield return null ; } SmartLogger . Log ( DebugFlags . WitAINonStreamingSpeechToText , " " + ( Time . time - startTime ) ) ; var responseJSON = new JSONObject ( request . response . Text , int . MaxValue ) ; SmartLogger . Log ( DebugFlags . WitAINonStreamingSpeechToText , " " ) ; SmartLogger . Log ( DebugFlags . WitAINonStreamingSpeechToText , responseJSON . ToString ( ) ) ; string errorText = WitAiSpeechToTextResponseJSONParser . GetErrorFromResponseJSON ( responseJSON ) ; if ( errorText != null ) { if ( m_OnError != null ) { m_OnError ( errorText ) ; } } if ( m_OnTextResult != null ) { m_OnTextResult ( WitAiSpeechToTextResponseJSONParser . GetTextResultFromResponseJSON ( responseJSON ) ) ; } m_TempAudioComponent . ClearTempAudioFiles ( ) ; }
Translates speech to text by making a request to the speech-to-text API.
Translates speech to text by making a request to the speech - to - text API .
// Save recorded audio to a WAV file. // Construct a request with the WAV file and send it. // Finally, grab the response JSON once the request is done.