如何将语音流向wit.ai语音终点

时间:2018-01-19 18:28:20

标签: c# httpwebrequest speech wit.ai

我无法从wit.ai的演讲结束点得到好的回应。响应总是400.我似乎遵循docs,但出了点问题。

任何帮助都将不胜感激。

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;
            }
  }

1 个答案:

答案 0 :(得分:0)

此代码示例使用正确的编码。如果您使用的是Naudio,请确保waveformat与编码类似(另外它必须是单声道):

  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;
            }
        }