sta / websocket-sharp

A C# implementation of the WebSocket protocol client and server
http://sta.github.io/websocket-sharp
MIT License
5.69k stars 1.66k forks source link

How to send audio through websocket-sharp. #373

Open vaibhavpancholi1 opened 7 years ago

vaibhavpancholi1 commented 7 years ago

i am trying below code. After opening i am sending audio but its failing .sent is coming false for audio.

how should i send it?

requirement is something like

using (var ws = new WebSocket(endpoint + "? format=Simple&language=en-US&Ocp-Apim-Subscription-Key=b37f5197677246609e15a445d7232d4a&X-ConnectionId=57AB5DB821D444EEA1A4C54FF8469A8A")) { ws.CustomHeaders = new Dictionary<string, string>() { { authInfo.HeaderName,authInfo.Token }, {ConnectionIdHeader,"57AB5DB821D444EEA1A4C54FF8469A8A" } }; ws.Origin = "https://speech.platform.bing.com"; ws.OnMessage += (sender, e) => {

            };
            ws.OnOpen += (sender, e) =>
            {
                Console.WriteLine("Connection opened");                
                string v = "path: speech.config x - requestid: 168920F9308C4F388E59C1E6E8F5F488 x - timestamp: 2017-06-07 content - type: application / json { 'context':{ 'system':{ 'version':'1.0.00000'},'os':{ 'platform':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.96 Safari/537.36','name':'Browser','version':null},'device':{ 'manufacturer':'SpeechSample','model':'SpeechSample','version':'1.0.00000'} } }";
                ws.Send(v);
                Console.WriteLine("speech config sent.");
                //   ws.Send("path: audio x - requestid: 168920F9308C4F388E59C1E6E8F5F488 x - timestamp: 2017-06-07 content - type: PCM");
                //byte[] bytes = System.IO.File.ReadAllBytes(@"C:\Users\615114\Desktop\websocket-sharp-master\Example3\test8.wav");
                //// double splitlength= (((double)bytes.Length) / (double)8192);
                //IEnumerable<byte[]> enume = bytes.Split(8192);
                //foreach (var item in enume)
                //{
                //    ws.Send(item);
                //}

                using (var istream = File.OpenRead(@"C:\Users\615114\Desktop\websocket-sharp-master\Example3\test8.wav"))
                {
                    byte[] buffer = new byte[2048];
                    int bytesRead;
                    while ((bytesRead = istream.Read(buffer, 0, 2048)) > 0)
                    {
                        //   ws.Send(new ArraySegment<byte>(buffer));
                        ws.Send(buffer);
                    }
                }

            };

            ws.OnMessage += (sender, e) =>
            {

            };
            ws.OnError += (sender, e) =>
            {
                //  Console.WriteLine(e);
            };
            ws.Connect();

            //   Console.ReadKey(true);
        }
compujuckel commented 7 years ago

If this is your real Bing subscription key you should remove it! There are also other problems with your code, e.g. spaces in x - requestid which should be x-requestid, sending audio without headers, etc.

The Speech Recognition API docs can be found here: https://docs.microsoft.com/en-us/azure/cognitive-services/speech/api-reference-rest/websocketprotocol