public SynthesisServer(string subscription, string region, string voiceName, SpeechSynthesisOutputFormat outputFormat, int concurrency)
        {
            this.speechConfig = SpeechConfig.FromSubscription(subscription, region);

            // set your voice name
            speechConfig.SpeechSynthesisVoiceName = voiceName;

            // use mp3 format to reduce network transfer payload
            speechConfig.SetSpeechSynthesisOutputFormat(outputFormat);

            // pool should be a single instance to handle all request. In real scenario, this could be put as a static class member
            pool               = new SynthesizerPool(() => new SpeechSynthesizer(speechConfig, null), concurrency);
            latencyList        = new List <double>();
            processingTimeList = new List <double>();
        }
Пример #2
0
        public static void SpeechSynthesizeWithPool()
        {
            var speechConfig       = SpeechConfig.FromSubscription(subscriptionKey, region);
            var pool               = new SynthesizerPool(() => new SpeechSynthesizer(speechConfig, null));
            var latencyList        = new List <double>();
            var processingTimeList = new List <double>();

            for (var turn = 0; turn < 3; turn++)
            {
                Console.WriteLine("turn: {0}", turn);

                Parallel.For(0, 64, (i) =>
                {
                    var start       = DateTime.Now;
                    var synthesizer = pool.Get();
                    bool first      = true;

                    void SynthesizingEvent(object sender, SpeechSynthesisEventArgs eventArgs)
                    {
                        // receive streaming audio here.
                        if (!first)
                        {
                            return;
                        }
                        Console.WriteLine("First byte latency: {0}", DateTime.Now - start);
                        first = false;
                        if (turn > 0)
                        {
                            latencyList.Add((DateTime.Now - start).TotalMilliseconds);
                        }
                    }

                    synthesizer.Synthesizing += SynthesizingEvent;

                    var result = synthesizer.SpeakTextAsync($"today is a nice day. {turn}{i}").Result;
                    if (result.Reason == ResultReason.SynthesizingAudioCompleted)
                    {
                        if (turn > 0)
                        {
                            processingTimeList.Add((DateTime.Now - start).TotalMilliseconds);
                        }
                        synthesizer.Synthesizing -= SynthesizingEvent;
                        pool.Put(synthesizer);
                    }
                    else
                    {
                        var err = SpeechSynthesisCancellationDetails.FromResult(result);
                        Console.WriteLine(err.ToString());
                        synthesizer.Dispose();
                    }
                });

                Thread.Sleep(2000);
            }

            latencyList.Sort();
            Console.WriteLine("Average latency {0} ms", latencyList.Average());
            Console.WriteLine("Max latency {0} ms", latencyList.Max());
            Console.WriteLine("Min latency {0} ms", latencyList.Min());
            Console.WriteLine("90% latency {0} ms", latencyList[Convert.ToInt32(latencyList.Count * 0.9)]);
            Console.WriteLine("95% latency {0} ms", latencyList[Convert.ToInt32(latencyList.Count * 0.95)]);

            processingTimeList.Sort();
            Console.WriteLine("\nAverage processing time {0} ms", processingTimeList.Average());
            Console.WriteLine("Max processing time {0} ms", processingTimeList.Max());
            Console.WriteLine("Min processing time {0} ms", processingTimeList.Min());
            Console.WriteLine("90% processing time {0} ms", processingTimeList[Convert.ToInt32(processingTimeList.Count * 0.9)]);
            Console.WriteLine("95% processing time {0} ms", processingTimeList[Convert.ToInt32(processingTimeList.Count * 0.95)]);

            Console.WriteLine("Press the Enter key to exit.");
            Console.ReadLine();
        }