Search code examples
c#httpwebrequestspeechwit.ai

How to stream speech to wit.ai speech end point


I'm having trouble getting a good response from wit.ai's speech end point. The response is always 400. I seem to be following the docs but something's wrong.

Any help would be appreciated.

private string ProcessSpeechStream(Stream stream)
  {
            BinaryReader filereader = new BinaryReader(stream);
            byte[] arr = filereader.ReadBytes((Int32)stream.Length);
            filereader.Close();


            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://api.wit.ai/speech");
            request.SendChunked = true;
            request.Method = "POST";
            request.Headers["Authorization"] = "Bearer " + APIToken;
            request.ContentType  = "chunked"; 
            request.ContentLength = arr.Length;          
            var st = request.GetRequestStream();
            st.Write(arr, 0, arr.Length);
            st.Close();
            // Process the wit.ai response
            try
            {
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    StreamReader response_stream = new StreamReader(response.GetResponseStream());
                    return response_stream.ReadToEnd();
                }
                else
                {
                    Logger.AILogger.Log("Error: " + response.StatusCode.ToString());
                    return string.Empty;

                }
            }
            catch (Exception ex)
            {
                Logger.AILogger.Log("Error: " + ex.Message, ex);
                return string.Empty;
            }
  }

Solution

  • This code sample uses the correct encoding. If you are using Naudio make sure you waveformat is like the encoding (Plus it has to be mono):

      private string ProcessSpeechStream(Stream stream)
            {
    
                var ms = new MemoryStream();
                stream.CopyTo(ms);
                var arr = ms.ToArray();
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://api.wit.ai/speech");
                request.SendChunked = true;
                request.Method = "POST";
                request.Headers["Authorization"] = "Bearer " + APIToken;
                request.ContentType = "audio/raw;encoding=signed-integer;bits=16;rate=44100;endian=little";
                request.ContentLength = arr.Length;
                var st = request.GetRequestStream();
                st.Write(arr, 0, arr.Length);
                st.Close();
                // Process the wit.ai response
                try
                {
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        StreamReader response_stream = new StreamReader(response.GetResponseStream());
                        return response_stream.ReadToEnd();
                    }
    
                }
                catch (Exception ex)
                {
                    // use your own exception handling class
                    // Logger.AILogger.Log("Error: " + ex.Message, ex);
                    return string.Empty;
                }
            }