コード例 #1
0
        public static void Speak(string text)
        {
            Console.WriteLine("Starting Authtentication");
            AccessTokenInfo token;

            // Note: Sign up at http://www.projectoxford.ai to get a subscription key.  Search for Speech APIs from Azure Marketplace.  Use the subscription key as Client secret below.
            Authentication.Authentication auth = new Authentication.Authentication("recog-hack-luigi", "2147d5958bdf498fb41026f202486331");

            try
            {
                token = auth.GetAccessToken();
                Console.WriteLine("Token: {0}\n", token.access_token);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed authentication.");
                Console.WriteLine(ex.ToString());
                Console.WriteLine(ex.Message);
                return;
            }

            Console.WriteLine("Starting TTSSample request code execution.");

            string requestUri = "https://speech.platform.bing.com/synthesize";

            var cortana = new Synthesize(new Synthesize.InputOptions()
            {
                RequestUri = new Uri(requestUri),
                // Text to be spoken.
                Text      = text,
                VoiceType = Gender.Male,
                // Refer to the documentation for complete list of supported locales.
                Locale = "it-IT",
                // You can also customize the output voice. Refer to the documentation to view the different
                // voices that the TTS service can output.
                VoiceName = "Microsoft Server Speech Text to Speech Voice (it-IT, Cosimo, Apollo)",
                // Service can return audio in different output format.
                OutputFormat       = AudioOutputFormat.Riff16Khz16BitMonoPcm,
                AuthorizationToken = "Bearer " + token.access_token,
            });

            cortana.OnAudioAvailable += PlayAudio;
            cortana.OnError          += ErrorHandler;
            cortana.Speak(CancellationToken.None).Wait();
        }
コード例 #2
0
        public static string Interupt(string wavFileLocation)
        {
            var uriEndpoint = @"https://speech.platform.bing.com/recognize";

            //var binDirectoryPath = Server.MapPath("~/bin");
            //var wavFileLocation = string.Format(@"{0}\Assets\AudioFile.wav", binDirectoryPath);

            AccessTokenInfo token;
            string          headerValue;
            CogResponse     result;

            // Note: Sign up at http://www.projectoxford.ai to get a subscription key.  Search for Speech APIs from Azure Marketplace.
            // Use the subscription key as Client secret below.
            Authentication.Authentication auth = new Authentication.Authentication("recog-hack-luigi", "2147d5958bdf498fb41026f202486331");

            //string requestUri = args[0].Trim(new char[] { '/', '?' });
            string requestUri = uriEndpoint.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 = wavFileLocation;
            string     responseString;
            FileStream fs = null;

            try
            {
                token = auth.GetAccessToken();
                Console.WriteLine("Token: {0}\n", token.access_token);

                /*
                 * Create a header with the access_token property of the returned token
                 */
                headerValue = "Bearer " + token.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();
                    }

                    Console.WriteLine("Response:");
                    using (WebResponse response = request.GetResponse())
                    {
                        Console.WriteLine(((HttpWebResponse)response).StatusCode);

                        using (StreamReader sr = new StreamReader(response.GetResponseStream()))
                        {
                            return(sr.ReadToEnd());
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                Console.WriteLine(ex.Message);
            }
            return(null);
        }