如何将语音流式传输到 wit.ai 个语音结束点

How to stream speech to wit.ai speech end point

我无法从 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;
            }
  }

此代码示例使用了正确的编码。如果您使用的是 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;
            }
        }