0

So using Wit.ai I'm trying to use speech to text. I am using the Wit3D example from Github: https://github.com/afauch/wit3d/blob/master/Assets/UserScripts/Wit3D.cs

Recording of sound and saving to the .wav file works just fine. Sending the request to the server does not. The .wav file is valid as I get a response when manually making a request through Postman.

The request code looks like this:

string GetJSONText(string file)
{
    // get the file w/ FileStream
    FileStream filestream = new FileStream(file, FileMode.Open, FileAccess.Read);
    BinaryReader filereader = new BinaryReader(filestream);
    byte[] BA_AudioFile = filereader.ReadBytes((Int32)filestream.Length);
    filestream.Close();
    filereader.Close();
    //var bytes = File.ReadAllBytes(Path.Combine(Application.dataPath, "sample.wav"));

    // create an HttpWebRequest
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://api.wit.ai/speech?v=20160901");

    request.Method = "POST";
    request.Headers["Authorization"] = "Bearer 3XFWDOBVS65V5A2VZWZFBB2PHOKDWGOH";
    request.ContentType = "audio/wav";
    //request.Timeout = 10000;

    request.GetRequestStream().Write(BA_AudioFile, 0, BA_AudioFile.Length);

    // Process the wit.ai response
    try
    {
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        if (response.StatusCode == HttpStatusCode.OK)
        {
            print("Http went through ok");
            StreamReader response_stream = new StreamReader(response.GetResponseStream());
            return response_stream.ReadToEnd();
        }
        else
        {
            return "Error: " + response.StatusCode.ToString();
            return "HTTP ERROR";
        }
    }
    catch (Exception ex)
    {
        return "Error: " + ex.Message;
        return "HTTP ERROR";
    }
}

With or without putting a Timeout on the request I get the following Error message: "Error: The request timed-out"

Removing the line:

request.GetRequestStream().Write(BA_AudioFile, 0, BA_AudioFile.Length)

Will get me a response: Error: Error getting response stream (Write: The authentication or decryption has failed.) Which makes sense, because there is nothing to decrypt. My firewall doesn't seem to be the problem. Any ideas of why there is a time-out? Using different methods of getting a byte[] didn't fix it either.

EDIT: Putting the code in a normal Console application does work. So this seems to be a Unity issue.

Hespen
  • 1,384
  • 2
  • 17
  • 27

1 Answers1

1

Add this to top of your script:

using System.Collections.Generic;

Use this code.

public void SendRequest(string wavPath)
{
    if(!File.Exists(wavPath))
    {
        Debug.Log("Invalid wav path.");
        return;
    }
    StartCoroutine(SendRequestToWitAi(wavPath));
}

public IEnumerator SendRequestToWitAi(string wavPath)
{
    string API_KEY = "3XFWDOBVS65V5A2VZWZFBB2PHOKDWGOH";

    string url = "https://api.wit.ai/speech?v=20160526";

    byte[] postData = File.ReadAllBytes(wavPath);
    Dictionary<string, string> headers = new Dictionary<string, string>();
    headers["Content-Type"] = "audio/wav";
    headers["Authorization"] = "Bearer " + API_KEY;

    float timeSent = Time.time;
    WWW www = new WWW(url, postData, headers);
    yield return www;

    while (!www.isDone)
    {
        yield return null;
    }
    float duration = Time.time - timeSent;

    if (www.error != null && www.error.Length > 0)
    {
        Debug.Log("Error: " + www.error + " (" + duration + " secs)");
        yield break;
    }
    Debug.Log("Success (" + duration + " secs)");
    Debug.Log("Result: " + www.text);
}

Use a JSON parser to parse www.text value. The "_text" field contains the result text.

Wellenbrecher
  • 179
  • 1
  • 9