static void Main(string[] args)
        {
            Console.WriteLine("Starting Authtentication");
            AccessTokenInfo token;

            // Note: Sign up at http://www.projectoxford.ai for the client credentials.
            Authentication auth = new Authentication("Your ClientId goes here", "Your Client Secret goes here");

            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      = "Hi, how are you doing?",
                VoiceType = Gender.Female,
                // Refer to the documentation for complete list of supported locales.
                Locale = "en-US",
                // 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 (en-US, ZiraRUS)",
                // 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();
        }
Esempio n. 2
0
        private static void Main(string[] args)
        {
            String apikey = "";
            String input  = "";

            if (args.Length < 2)
            {
                Console.WriteLine("Usage: speak [API key] [What to say]");
                return;
            }
            else
            {
                apikey = args[0];
                for (int i = 1; i < args.Length; i++)
                {
                    input = input + args[i] + " ";
                }
                input = input.TrimEnd(' ');
            }

            Console.WriteLine("input=" + input);

            Console.WriteLine("Starting Authtentication");
            string accessToken;

            Authentication auth = new Authentication("https://api.cognitive.microsoft.com/sts/v1.0/issueToken", apikey);

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

            Console.WriteLine("Starting TTSSample request code execution.");
            // Synthesis endpoint for old Bing Speech API: https://speech.platform.bing.com/synthesize
            // For new unified SpeechService API: https://westus.tts.speech.microsoft.com/cognitiveservices/v1
            // Note: new unified SpeechService API synthesis endpoint is per region
            string requestUri = "https://speech.platform.bing.com/synthesize";
            var    cortana    = new Synthesize();

            cortana.OnAudioAvailable += PlayAudio;
            cortana.OnError          += ErrorHandler;

            // Reuse Synthesize object to minimize latency
            cortana.Speak(CancellationToken.None, new Synthesize.InputOptions()
            {
                RequestUri = new Uri(requestUri),
                // Text to be spoken.
                Text      = input,
                VoiceType = Gender.Female,
                // Refer to the documentation for complete list of supported locales.
                Locale = "zh-TW",
                // 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 (en-US, Jessa24KRUS)",
                VoiceName = "Microsoft Server Speech Text to Speech Voice (zh-TW, Yating, Apollo)",
                // VoiceName = "Microsoft Server Speech Text to Speech Voice (en-US, ZiraRUS)",

                // Service can return audio in different output format.
                OutputFormat       = AudioOutputFormat.Riff24Khz16BitMonoPcm,
                AuthorizationToken = "Bearer " + accessToken,
            }).Wait();
        }
Esempio n. 3
0
        private static void SpeakWithVoice(string tokenUri, string endpointUri, string key, string locale, string voiceName,
                                           AudioOutputFormat format, string text = "Hello, how are you doing?",
                                           string outputFile = null)
        {
            string accessToken = string.Empty;

            // The way to get api key:
            // Unified Speech Service key
            // Free: https://azure.microsoft.com/en-us/try/cognitive-services/?api=speech-services
            // Paid: https://go.microsoft.com/fwlink/?LinkId=872236&clcid=0x409

            if (tokenUri != null)
            {
                Console.WriteLine("Starting Authtentication");
                Authentication auth = new Authentication(tokenUri, key);

                try
                {
                    accessToken = auth.GetAccessToken();
                    Console.WriteLine("Token: {0}\n", accessToken);
                }
                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 = endpointUri;
            var    cortana    = new Synthesize();

            System.EventHandler <GenericEventArgs <Stream> > handler;
            if (outputFile == null)
            {
                handler = PlayAudio;
            }
            else
            {
                handler = (sender, e) => SaveAudio(sender, e, outputFile);
            }

            cortana.OnAudioAvailable += handler;
            cortana.OnError          += ErrorHandler;

            // Reuse Synthesize object to minimize latency
            cortana.Speak(CancellationToken.None, new Synthesize.InputOptions()
            {
                RequestUri = new Uri(requestUri),
                // Text to be spoken.
                Text      = text,
                VoiceType = Gender.Female,

                // Refer to the documentation for complete list of supported locales.
                // Please note locale must be matched with voice locales.
                Locale       = locale,
                VoiceName    = voiceName,
                OutputFormat = format,

                // For onpremise container, auth token is optional
                AuthorizationToken = "Bearer " + accessToken,
            }).Wait();

            cortana.OnAudioAvailable -= handler;
            cortana.OnError          -= ErrorHandler;
        }
        private static void HttpClientTest(string accessToken, string[] sentences, string locale, string voiceName)
        {
            string requestUri = "https://speech.platform.bing.com/synthesize";

            Console.WriteLine("===Http client code test===");
            var cortana = new Synthesize();

            cortana.OnAudioAvailable += PlayAudio;
            cortana.OnError          += ErrorHandler;

            cortana.Speak(CancellationToken.None, new Synthesize.InputOptions()
            {
                RequestUri = new Uri(requestUri),
                // Text to speak
                Text      = "Hello this is a warmup.",
                VoiceType = Gender.Female,
                // Refer to the documentation for complete list of supported locales.
                Locale = locale,
                // You can also customize the output voice. Refer to the documentation to view the different
                // voices that the TTS service can output.
                VoiceName          = voiceName,
                AuthorizationToken = "Bearer " + accessToken,
            }).Wait();

            double[]     wavLength   = new double[sentences.Length];
            var          table       = new StringBuilder("AudioFormat\tAvg. First Byte Latency\tAvg. Last Byte Latency\n");
            Queue <long> requestTime = new Queue <long>();

            foreach (var outputFormat in Enum.GetValues(typeof(AudioOutputFormat)).Cast <AudioOutputFormat>())
            {
                var outputFormatName = Enum.GetName(typeof(AudioOutputFormat), outputFormat);
                Console.WriteLine("AudioFormat = {0}", outputFormatName);
                var count = 0;
                for (var i = 0; i < 10; i++)
                {
                    foreach (var text in sentences)
                    {
                        // Standard S0 pricing tier allows 20 calls/second
                        // Use 19 here just to be safe
                        while (requestTime.Count >= 19)
                        {
                            while (requestTime.Count > 0 && DateTime.Now.Ticks - requestTime.Peek() > 60 * 1000)
                            {
                                requestTime.Dequeue();
                            }
                            Thread.Sleep(1000);
                        }

                        requestTime.Enqueue(DateTime.Now.Ticks);

                        Console.WriteLine("Test " + (++count));
                        outputFilename = String.Format("{0}-{1}", outputFormatName, count);
                        sw.Restart();
                        cortana.Speak(CancellationToken.None, new Synthesize.InputOptions()
                        {
                            RequestUri = new Uri(requestUri),
                            // Text to speak
                            Text      = text,
                            VoiceType = Gender.Female,
                            // Refer to the documentation for complete list of supported locales.
                            Locale = locale,
                            // You can also customize the output voice. Refer to the documentation to view the different
                            // voices that the TTS service can output.
                            VoiceName = voiceName,
                            // Service can return audio in different output format.
                            OutputFormat       = outputFormat,
                            AuthorizationToken = "Bearer " + accessToken,
                        }).Wait();
                        if (count <= sentences.Length && outputFormat == AudioOutputFormat.Riff16Khz16BitMonoPcm)
                        {
                            wavLength[count - 1] = bytesRead[count - 1] / 32;
                        }
                    }
                }
                var avgFBL = firstByteLatency.Average();
                firstByteLatency.Clear();
                var avgLBL = lastByteLatency.Average();
                lastByteLatency.Clear();
                var avgBytes = bytesRead.Average();
                bytesRead.Clear();
                Console.WriteLine("Average First Byte Latency: {0} ms", avgFBL);
                Console.WriteLine("Average Last Byte Latency: {0} ms", avgLBL);
                table.AppendLine(outputFormatName + "\t" + avgFBL + "\t" + avgLBL);
            }
            Console.WriteLine(table);
            Console.WriteLine("Average Wav Length: {0} ms", wavLength.Average());
        }
Esempio n. 5
0
        static void Main(string[] args)
        {
            Console.WriteLine("Starting Authtentication");
            string accessToken;

            var builder = new ConfigurationBuilder()
                          .SetBasePath(Directory.GetCurrentDirectory())
                          .AddJsonFile("appsettings.json", true);

            var devEnvironmentVariable = Environment.GetEnvironmentVariable("NETCORE_ENVIRONMENT");

            var isDevelopment = string.IsNullOrEmpty(devEnvironmentVariable) ||
                                devEnvironmentVariable.ToLower() == "development";

            if (isDevelopment)
            {
                builder.AddUserSecrets <Program>();
            }

            IConfiguration config = builder.Build();

            // The way to get api key:
            // Unified Speech Service key
            // Free: https://azure.microsoft.com/en-us/try/cognitive-services/?api=speech-services
            // Paid: https://go.microsoft.com/fwlink/?LinkId=872236&clcid=0x409
            Authentication auth = new Authentication(config["key"]);

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

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

            // Note: new unified SpeechService API synthesis endpoint is per region, choose the region close to your service to minimize the latency
            // Also must use the same region in request as authentication, e.g. both westus
            string requestUri = "https://westeurope.tts.speech.microsoft.com/cognitiveservices/v1";

            var cortana = new Synthesize(new Synthesize.InputOptions()
            {
                RequestUri = new Uri(requestUri),
                // Text to be spoken.
                Text      = "Carlos, please go faster! People is hungry and you need to show the last example!",
                VoiceType = Gender.Female,
                // Refer to the documentation for complete list of supported locales.
                Locale = "en-US",
                // 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 (en-US, ZiraRUS)",
                VoiceName = "Microsoft Server Speech Text to Speech Voice (en-US, Jessa24KRUS)",

                // Service can return audio in different output format.
                OutputFormat       = AudioOutputFormat.Riff24Khz16BitMonoPcm,
                AuthorizationToken = "Bearer " + accessToken,
            });

            cortana.OnAudioAvailable += StoreAudio;
            cortana.OnError          += ErrorHandler;
            cortana.Speak(CancellationToken.None).Wait();
        }
        private static void Main(string[] args)
        {
            Console.WriteLine("Starting Authtentication");
            string accessToken;

            // Issue token uri for old Bing Speech API "https://api.cognitive.microsoft.com/sts/v1.0/issueToken";
            // Issue token uri for new unified SpeechService API "https://westus.api.cognitive.microsoft.com/sts/v1.0/issueToken".
            // Note: new unified SpeechService API key and issue token uri is per region

            // The way to get api key:
            // Old Bing Speech key
            // Free: https://www.microsoft.com/cognitive-services/en-us/subscriptions?productId=/products/Bing.Speech.Preview
            // Paid: https://portal.azure.com/#create/Microsoft.CognitiveServices/apitype/Bing.Speech/pricingtier/S0
            // New unified SpeechService key
            // Free: https://azure.microsoft.com/en-us/try/cognitive-services/?api=speech-services
            // Paid: https://go.microsoft.com/fwlink/?LinkId=872236&clcid=0x409
            Authentication auth = new Authentication("https://westus.api.cognitive.microsoft.com/sts/v1.0/issueToken", "input your key here");

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

            Console.WriteLine("Starting TTSSample request code execution.");
            // Synthesis endpoint for old Bing Speech API: https://speech.platform.bing.com/synthesize
            // For new unified SpeechService API: https://westus.tts.speech.microsoft.com/cognitiveservices/v1
            // Note: new unified SpeechService API synthesis endpoint is per region
            string requestUri = "https://westus.tts.speech.microsoft.com/cognitiveservices/v1";
            var    cortana    = new Synthesize();

            cortana.OnAudioAvailable += PlayAudio;
            cortana.OnError          += ErrorHandler;

            // Reuse Synthesize object to minimize latency
            cortana.Speak(CancellationToken.None, new Synthesize.InputOptions()
            {
                RequestUri = new Uri(requestUri),
                // Text to be spoken.
                Text      = "Hello, how are you doing?",
                VoiceType = Gender.Female,
                // Refer to the documentation for complete list of supported locales.
                Locale = "en-US",
                // 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 (en-US, Jessa24KRUS)",
                VoiceName = "Microsoft Server Speech Text to Speech Voice (en-US, Guy24KRUS)",
                // VoiceName = "Microsoft Server Speech Text to Speech Voice (en-US, ZiraRUS)",

                // Service can return audio in different output format.
                OutputFormat       = AudioOutputFormat.Riff24Khz16BitMonoPcm,
                AuthorizationToken = "Bearer " + accessToken,
            }).Wait();
        }
Esempio n. 7
0
        static void Main(string[] args)
        {
            Console.WriteLine("Starting Authtentication");
            string accessToken;

            // The way to get api key:
            // Unified Speech Service key
            // Free: https://azure.microsoft.com/en-us/try/cognitive-services/?api=speech-services
            // Paid: https://go.microsoft.com/fwlink/?LinkId=872236&clcid=0x409
            Authentication auth = new Authentication("Your API Key Goes Here");

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

            Console.WriteLine("Starting TTSSample request code execution...");
            string text = string.Empty;

            do
            {
                Console.WriteLine("Please type a sentence or press Enter to exit.");
                text = Console.ReadLine();
                if (text == string.Empty)
                {
                    return;
                }

                // Note: new unified SpeechService API synthesis endpoint is per region, choose the region close to your service to minimize the latency
                // Also must use the same region in request as authentication, e.g. both westus
                string requestUri = "https://westus.tts.speech.microsoft.com/cognitiveservices/v1";

                var synthInputOptions = new Synthesize.InputOptions()
                {
                    RequestUri = new Uri(requestUri),
                    // Text to be spoken.
                    Text = text,
                    //Text = "Good morning Chelley, how are you doing?",
                    VoiceType = Gender.Female,
                    // Refer to the documentation for complete list of supported locales.
                    Locale = "en-US",
                    // 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 (en-US, ZiraRUS)",
                    //VoiceName = "Microsoft Server Speech Text to Speech Voice (en-US, Jessa24KRUS)",

                    // Service can return audio in different output format.
                    OutputFormat       = AudioOutputFormat.Riff24Khz16BitMonoPcm,
                    AuthorizationToken = "Bearer " + accessToken,
                };

                var cortana = new Synthesize(synthInputOptions);
                cortana.OnAudioAvailable += StoreAudio;
                cortana.OnError          += ErrorHandler;
                cortana.Speak(CancellationToken.None).Wait();
            } while (true);
        }