Exemplo n.º 1
0
        public static IEnumerable<string> Convert(string filename)
        {
            if (!File.Exists(filename))
            {
                throw new FileNotFoundException("Not found.", filename);
            }

            AdmAccessToken admToken;
            string headerValue;

            /*
             * Note: Create an App using the ADM portal by signing up for an account at https://datamarket.azure.com/home and then go to https://datamarket.azure.com/developer/applications and click register.  Put in some unique clientId and a client secret will be generated for you.
             */
            AdmAuthentication admAuth = new AdmAuthentication("autotranscribe", "yKrGbpOOzCbKSl5N+/PLjIiUgU1KpQHQJIabKI2AR/c=");

            string requestUri = "https://speech.platform.bing.com/recognize".Trim(new char[] {'/', '?'});

            /* URI Params. Refer to the README file for more information. */
            requestUri += @"?scenarios=smd";                                  // websearch is the other main option.
            requestUri += @"&appid=D4D52672-91D7-4C74-8AD8-42B1D98141A5";     // You must use this ID.
            requestUri += @"&locale=en-US";                                   // We support several other languages.  Refer to README file.
            requestUri += @"&device.os=wp7";
            requestUri += @"&version=3.0";
            requestUri += @"&format=json";
            requestUri += @"&instanceid=565D69FF-E928-4B7E-87DA-9A750B96D9E3";
            requestUri += @"&requestid=" + Guid.NewGuid().ToString();

            string host = @"speech.platform.bing.com";
            string contentType = @"audio/wav; codec=""audio/pcm""; samplerate=16000";

            /*
             * Input your own audio file or use read from a microphone stream directly.
             */
            string audioFile = filename;
            string responseString;
            FileStream fs = null;
            var transcription = new List<string>();
            try
            {
                admToken = admAuth.GetAccessToken();
                Console.WriteLine("ADM Token: {0}\n", admToken.access_token);

                /*
                 * Create a header with the access_token property of the returned token
                 */
                headerValue = "Bearer " + admToken.access_token;

                Console.WriteLine("Request Uri: " + requestUri + Environment.NewLine);

                HttpWebRequest request = null;
                request = (HttpWebRequest)HttpWebRequest.Create(requestUri);
                request.SendChunked = true;
                request.Accept = @"application/json;text/xml";
                request.Method = "POST";
                request.ProtocolVersion = HttpVersion.Version11;
                request.Host = host;
                request.ContentType = contentType;
                request.Headers["Authorization"] = headerValue;
                request.Timeout = 100000*5;

                using (fs = new FileStream(audioFile, FileMode.Open, FileAccess.Read))
                {

                    /*
                     * Open a request stream and write 1024 byte chunks in the stream one at a time.
                     */
                    byte[] buffer = null;
                    int bytesRead = 0;
                    using (Stream requestStream = request.GetRequestStream())
                    {
                        /*
                         * Read 1024 raw bytes from the input audio file.
                         */
                        buffer = new Byte[checked((uint)Math.Min(1024, (int)fs.Length))];
                        while ((bytesRead = fs.Read(buffer, 0, buffer.Length)) != 0)
                        {
                            requestStream.Write(buffer, 0, bytesRead);
                        }

                        // Flush
                        requestStream.Flush();
                    }

                    /*
                     * Get the response from the service.
                     */
                    //Console.WriteLine("Response:");
                    using (WebResponse response = request.GetResponse())
                    {
                        Console.WriteLine(((HttpWebResponse)response).StatusCode);

                        using (StreamReader sr = new StreamReader(response.GetResponseStream()))
                        {
                            responseString = sr.ReadToEnd();
                        }
                        //Convert response to JSON object.
                        dynamic stuff = JsonConvert.DeserializeObject(responseString);
                        var header = stuff["header"];
                        var success = header["status"];
                        if (success.ToString() == "success")
                        {
                            var results = stuff["results"];
                            var data = results[0]["name"];
                            transcription.Add(data.ToString());
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                Console.WriteLine(ex.Message);
                System.Diagnostics.Debug.WriteLine(ex);
                return new string[0];
            }
            return transcription.ToArray();
        }
        public static dynamic requestBingVoice(string filename)
        {
            string endpoint = "https://speech.platform.bing.com/recognize";



            AdmAccessToken admToken;
            string         headerValue;

            /*
             * Note: Create an App using the ADM portal by signing up for an account at https://datamarket.azure.com/home and then go to https://datamarket.azure.com/developer/applications and click register.  Put in some unique clientId and a client secret will be generated for you.
             */
            AdmAuthentication admAuth = new AdmAuthentication("mhorowitzgelb", "oLCasG+Ahd2PrcHEMhdbBXrEUbaG7MWXdaFKLiwbXxE=");

            string requestUri = endpoint.Trim(new char[] { '/', '?' });

            /* URI Params. Refer to the README file for more information. */
            requestUri += @"?scenarios=smd";                                  // websearch is the other main option.
            requestUri += @"&appid=D4D52672-91D7-4C74-8AD8-42B1D98141A5";     // You must use this ID.
            requestUri += @"&locale=en-US";                                   // We support several other languages.  Refer to README file.
            requestUri += @"&device.os=wp7";
            requestUri += @"&version=3.0";
            requestUri += @"&format=json";
            requestUri += @"&instanceid=565D69FF-E928-4B7E-87DA-9A750B96D9E3";
            requestUri += @"&requestid=" + Guid.NewGuid().ToString();

            string host        = @"speech.platform.bing.com";
            string contentType = @"audio/wav; codec=""audio/pcm""; samplerate=16000";

            /*
             * Input your own audio file or use read from a microphone stream directly.
             */
            FileStream fs = null;
            string     responseString;

            try
            {
                admToken = admAuth.GetAccessToken();
                Console.WriteLine("ADM Token: {0}\n", admToken.access_token);

                /*
                 * Create a header with the access_token property of the returned token
                 */
                headerValue = "Bearer " + admToken.access_token;

                Console.WriteLine("Request Uri: " + requestUri + Environment.NewLine);

                HttpWebRequest request = null;
                request                          = (HttpWebRequest)HttpWebRequest.Create(requestUri);
                request.SendChunked              = true;
                request.Accept                   = @"application/json;text/xml";
                request.Method                   = "POST";
                request.ProtocolVersion          = HttpVersion.Version11;
                request.Host                     = host;
                request.ContentType              = contentType;
                request.Headers["Authorization"] = headerValue;



                using (fs = new FileStream(filename, FileMode.Open, FileAccess.Read)){
                    /*
                     * Open a request stream and write 1024 byte chunks in the stream one at a time.
                     */
                    byte[] buffer    = null;
                    int    bytesRead = 0;
                    using (Stream requestStream = request.GetRequestStream())
                    {
                        /*
                         * Read 1024 raw bytes from the input audio file.
                         */
                        buffer = new Byte[checked ((uint)Math.Min(1024, (int)fs.Length))];
                        while ((bytesRead = fs.Read(buffer, 0, buffer.Length)) != 0)
                        {
                            requestStream.Write(buffer, 0, bytesRead);
                        }

                        // Flush
                        requestStream.Flush();
                    }
                }

                /*
                 * Get the response from the service.
                 */
                Console.WriteLine("Response:");
                using (WebResponse response = request.GetResponse())
                {
                    Console.WriteLine(((HttpWebResponse)response).StatusCode);

                    using (StreamReader sr = new StreamReader(response.GetResponseStream()))
                    {
                        responseString = sr.ReadToEnd();
                    }


                    JavaScriptSerializer js = new JavaScriptSerializer();

                    Console.WriteLine("Json:");
                    Console.WriteLine(responseString);
                    return(js.Deserialize <dynamic>(responseString));
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                Console.WriteLine(ex.Message);
                return(null);
            }
        }
Exemplo n.º 3
0
        public static IEnumerable <string> Convert(string filename)
        {
            if (!File.Exists(filename))
            {
                throw new FileNotFoundException("Not found.", filename);
            }

            AdmAccessToken admToken;
            string         headerValue;

            /*
             * Note: Create an App using the ADM portal by signing up for an account at https://datamarket.azure.com/home and then go to https://datamarket.azure.com/developer/applications and click register.  Put in some unique clientId and a client secret will be generated for you.
             */
            AdmAuthentication admAuth = new AdmAuthentication("autotranscribe", "yKrGbpOOzCbKSl5N+/PLjIiUgU1KpQHQJIabKI2AR/c=");

            string requestUri = "https://speech.platform.bing.com/recognize".Trim(new char[] { '/', '?' });

            /* URI Params. Refer to the README file for more information. */
            requestUri += @"?scenarios=smd";                                  // websearch is the other main option.
            requestUri += @"&appid=D4D52672-91D7-4C74-8AD8-42B1D98141A5";     // You must use this ID.
            requestUri += @"&locale=en-US";                                   // We support several other languages.  Refer to README file.
            requestUri += @"&device.os=wp7";
            requestUri += @"&version=3.0";
            requestUri += @"&format=json";
            requestUri += @"&instanceid=565D69FF-E928-4B7E-87DA-9A750B96D9E3";
            requestUri += @"&requestid=" + Guid.NewGuid().ToString();

            string host        = @"speech.platform.bing.com";
            string contentType = @"audio/wav; codec=""audio/pcm""; samplerate=16000";


            /*
             * Input your own audio file or use read from a microphone stream directly.
             */
            string     audioFile = filename;
            string     responseString;
            FileStream fs            = null;
            var        transcription = new List <string>();

            try
            {
                admToken = admAuth.GetAccessToken();
                Console.WriteLine("ADM Token: {0}\n", admToken.access_token);

                /*
                 * Create a header with the access_token property of the returned token
                 */
                headerValue = "Bearer " + admToken.access_token;

                Console.WriteLine("Request Uri: " + requestUri + Environment.NewLine);

                HttpWebRequest request = null;
                request                          = (HttpWebRequest)HttpWebRequest.Create(requestUri);
                request.SendChunked              = true;
                request.Accept                   = @"application/json;text/xml";
                request.Method                   = "POST";
                request.ProtocolVersion          = HttpVersion.Version11;
                request.Host                     = host;
                request.ContentType              = contentType;
                request.Headers["Authorization"] = headerValue;
                request.Timeout                  = 100000 * 5;

                using (fs = new FileStream(audioFile, FileMode.Open, FileAccess.Read))
                {
                    /*
                     * Open a request stream and write 1024 byte chunks in the stream one at a time.
                     */
                    byte[] buffer    = null;
                    int    bytesRead = 0;
                    using (Stream requestStream = request.GetRequestStream())
                    {
                        /*
                         * Read 1024 raw bytes from the input audio file.
                         */
                        buffer = new Byte[checked ((uint)Math.Min(1024, (int)fs.Length))];
                        while ((bytesRead = fs.Read(buffer, 0, buffer.Length)) != 0)
                        {
                            requestStream.Write(buffer, 0, bytesRead);
                        }

                        // Flush
                        requestStream.Flush();
                    }

                    /*
                     * Get the response from the service.
                     */
                    //Console.WriteLine("Response:");
                    using (WebResponse response = request.GetResponse())
                    {
                        Console.WriteLine(((HttpWebResponse)response).StatusCode);

                        using (StreamReader sr = new StreamReader(response.GetResponseStream()))
                        {
                            responseString = sr.ReadToEnd();
                        }
                        //Convert response to JSON object.
                        dynamic stuff   = JsonConvert.DeserializeObject(responseString);
                        var     header  = stuff["header"];
                        var     success = header["status"];
                        if (success.ToString() == "success")
                        {
                            var results = stuff["results"];
                            var data    = results[0]["name"];
                            transcription.Add(data.ToString());
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                Console.WriteLine(ex.Message);
                System.Diagnostics.Debug.WriteLine(ex);
                return(new string[0]);
            }
            return(transcription.ToArray());
        }
Exemplo n.º 4
0
        static void Main(string[] args)
        {
            if ((args.Length < 2) || (string.IsNullOrWhiteSpace(args[0])))
            {
                Console.WriteLine("Arg[0]: Specify the endpoint to hit https://speech.platform.bing.com/recognize");
                Console.WriteLine("Arg[1]: Specify a valid input wav file.");

                return;
            }

            AdmAccessToken admToken;
            string headerValue;

            /*
             * Note: Create an App using the ADM portal by signing up for an account at https://datamarket.azure.com/home and then go to https://datamarket.azure.com/developer/applications and click register.  Put in some unique clientId and a client secret will be generated for you.
             */
            AdmAuthentication admAuth = new AdmAuthentication("Your ADM App ClientId goes here", "Your ADM App Client Secret goes here");

            string requestUri = args[0].Trim(new char[] {'/', '?'});

            /* URI Params. Refer to the README file for more information. */
            requestUri += @"?scenarios=smd";                                  // websearch is the other main option.
            requestUri += @"&appid=D4D52672-91D7-4C74-8AD8-42B1D98141A5";     // You must use this ID.
            requestUri += @"&locale=en-US";                                   // We support several other languages.  Refer to README file.
            requestUri += @"&device.os=wp7";
            requestUri += @"&version=3.0";
            requestUri += @"&format=json";
            requestUri += @"&instanceid=565D69FF-E928-4B7E-87DA-9A750B96D9E3";
            requestUri += @"&requestid=" + Guid.NewGuid().ToString();

            string host = @"speech.platform.bing.com";
            string contentType = @"audio/wav; codec=""audio/pcm""; samplerate=16000";

            /*
             * Input your own audio file or use read from a microphone stream directly.
             */
            string audioFile = args[1];
            string responseString;
            FileStream fs = null;

            try
            {
                admToken = admAuth.GetAccessToken();
                Console.WriteLine("ADM Token: {0}\n", admToken.access_token);

                /*
                 * Create a header with the access_token property of the returned token
                 */
                headerValue = "Bearer " + admToken.access_token;

                Console.WriteLine("Request Uri: " + requestUri + Environment.NewLine);

                HttpWebRequest request = null;
                request = (HttpWebRequest)HttpWebRequest.Create(requestUri);
                request.SendChunked = true;
                request.Accept = @"application/json;text/xml";
                request.Method = "POST";
                request.ProtocolVersion = HttpVersion.Version11;
                request.Host = host;
                request.ContentType = contentType;
                request.Headers["Authorization"] = headerValue;

                using (fs = new FileStream(audioFile, FileMode.Open, FileAccess.Read))
                {

                    /*
                     * Open a request stream and write 1024 byte chunks in the stream one at a time.
                     */
                    byte[] buffer = null;
                    int bytesRead = 0;
                    using (Stream requestStream = request.GetRequestStream())
                    {
                        /*
                         * Read 1024 raw bytes from the input audio file.
                         */
                        buffer = new Byte[checked((uint)Math.Min(1024, (int)fs.Length))];
                        while ((bytesRead = fs.Read(buffer, 0, buffer.Length)) != 0)
                        {
                            requestStream.Write(buffer, 0, bytesRead);
                        }

                        // Flush
                        requestStream.Flush();
                    }

                    /*
                     * Get the response from the service.
                     */
                    Console.WriteLine("Response:");
                    using (WebResponse response = request.GetResponse())
                    {
                        Console.WriteLine(((HttpWebResponse)response).StatusCode);

                        using (StreamReader sr = new StreamReader(response.GetResponseStream()))
                        {
                            responseString = sr.ReadToEnd();
                        }

                        Console.WriteLine(responseString);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                Console.WriteLine(ex.Message);
            }
        }