1

I try to set request to recognize wav file->to text with Yandex Speech API via desktop app.

Example of request from documentation:

POST /asr_xml?uuid=01ae13cb744628b58fb536d496daa1e6&key=developers-simple- key&topic=maps HTTP/1.1
Content-Type: audio/x-speex
User-Agent: Dalvik/1.2.0 (Linux; U; Android 2.2.2; LG-P990 Build/FRG83G)
Host: asr.yandex.net
Transfer-Encoding: chunked

7d0
...
chunked body

So, i register at developer forum, get api key and write simple code:

public string RecognizeSpeech(byte[] bytes, String uuid, String apiKey, string topic = "queries", string lang = "ru-RU")
    {
        try
        {
            var uri = string.Format("https://asr.yandex.net/asr_xml?" +
            "uuid={0}" +
            "&key={1}" +
            "&topic={2}" +
            "&lang={3}", uuid, apiKey, topic, lang);

            var request = (HttpWebRequest)WebRequest.Create(uri);
            request.Method = "POST";
            request.ContentType = "audio/x-wav";//"audio/x-pcm;bit=16;rate=16000";
            request.ContentLength = bytes.Length;


            using (var stream = request.GetRequestStream())
            {
                stream.Write(bytes, 0, bytes.Length);// exception here
            }

            var response = request.GetResponse();

            var reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            return reader.ReadToEnd();
        }
        catch (Exception ex)
        {
          ...
        }
        return "";
    }

I try to use audio/x-wav or audio/x-pcm;bit=16;rate=16000, but I get error:

Unable to write data to the transport connection: remote host forcibly ripped existing connection.

(use google translate)

byte[] bytes- is:

var audioBytes = File.ReadAllBytes(@"file.wav");

P.S. documentation on Russian language

user2545071
  • 1,408
  • 2
  • 24
  • 46

1 Answers1

1

This gist set POST request to Yandex Speech Cloud.

You need to make valid audio file (i use Freemake) - pcm,16 bit, 16000 hz, mono channel. Here the solution:

public string PostMethod(byte[] bytes)
    {
        string postUrl = "https://asr.yandex.net/asr_xml?" +
        "uuid=01ae13cb744628b58fb536d496daa1e6&" +
        "key="+your_api_key_here+"&" +
        "topic=queries";

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(postUrl);
        request.Method = "POST";
        request.Host = "asr.yandex.net";
        request.SendChunked = true;
        request.UserAgent = "Oleg";

        request.ContentType = "audio/x-pcm;bit=16;rate=16000";
        request.ContentLength = bytes.Length;

        using (var newStream = request.GetRequestStream())
        {
            newStream.Write(bytes, 0, bytes.Length);
        }

        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        string responseToString = "";
        if (response != null)
        {
            var strreader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            responseToString = strreader.ReadToEnd();
        }

        int index = responseToString.IndexOf("<variant confidence=\"1\">");

        responseToString = responseToString.Substring(index + 24, responseToString.Length - index - 24);

        int index2 = responseToString.IndexOf("</variant>");

        responseToString = responseToString.Substring(0, index2);

        return responseToString;
    }
user2545071
  • 1,408
  • 2
  • 24
  • 46