Beispiel #1
0
        private static async Task SynthesizeSpeechAsync(IConfiguration config, FileInfo textFile)
        {
            var text = await File.ReadAllLinesAsync(textFile.FullName);

            int contineWith = 0;
            int counter     = contineWith;

            foreach (var page in text.Page(25).Skip(contineWith))
            {
                var outputFile = Path.Combine(textFile.DirectoryName, $"{Path.GetFileNameWithoutExtension(textFile.Name)}{counter:D4}.wav");

                Log($"synthesizing page {counter}", ConsoleColor.Gray);

                var speechConfig = SpeechConfig.FromSubscription(config["SubscriptionKey"], config["Region"]);
                using var speech = new SpeechSynthesizer(speechConfig,
                                                         AutoDetectSourceLanguageConfig.FromOpenRange(),
                                                         AudioConfig.FromWavFileOutput(outputFile));

                string textToConvert = string.Join(Environment.NewLine, page);
                var    result        = await speech.SpeakTextAsync(textToConvert);

                if (result.Reason != ResultReason.SynthesizingAudioCompleted)
                {
                    throw new Exception(result.Reason.ToString());
                }

                counter++;
            }
        }
Beispiel #2
0
        public async Task <string> SynthesisToSpeakerAsync(string text)
        {
            var config = GetClient();
            var sb     = new StringBuilder();


            using var synthesizer = new SpeechSynthesizer(config,
                                                          AutoDetectSourceLanguageConfig.FromOpenRange(),
                                                          AudioConfig.FromDefaultSpeakerOutput());

            using var result = await synthesizer.SpeakTextAsync(text);

            if (result.Reason == ResultReason.SynthesizingAudioCompleted)
            {
                sb.AppendLine($"Speech synthesized to speaker for text [{text}]");
            }
            else if (result.Reason == ResultReason.Canceled)
            {
                var cancellation = SpeechSynthesisCancellationDetails.FromResult(result);
                sb.AppendLine($"CANCELED: Reason={cancellation.Reason}");

                if (cancellation.Reason == CancellationReason.Error)
                {
                    sb.AppendLine($"CANCELED: ErrorCode={cancellation.ErrorCode}");
                    sb.AppendLine($"CANCELED: ErrorDetails=[{cancellation.ErrorDetails}]");
                    sb.AppendLine($"CANCELED: Did you update the subscription info?");
                }
            }
            return(sb.ToString());
        }
Beispiel #3
0
        async Task <byte[]> SynthesizeAudioAsync(string msg)
        {
            var response        = new byte[] { };
            var autoDetecConfig = AutoDetectSourceLanguageConfig.FromOpenRange();

            using var synthesizer = new SpeechSynthesizer(_speechConfig, autoDetecConfig, AudioConfig.FromDefaultSpeakerOutput());
            using var result      = await synthesizer.SpeakTextAsync(msg);

            if (result.Reason == ResultReason.SynthesizingAudioCompleted)
            {
                response = result.AudioData;
            }
            return(response);
        }
Beispiel #4
0
        // Speech synthesis with auto detection for source language
        // Note: this is a preview feature, which might be updated in future versions.
        public static async Task SynthesisWithAutoDetectSourceLanguageAsync()
        {
            // Creates an instance of a speech config with specified subscription key and service region.
            // Replace with your own subscription key and service region (e.g., "westus").
            // The default language is "en-us".
            var config = SpeechConfig.FromSubscription("YourSubscriptionKey", "YourServiceRegion");

            // Creates an instance of AutoDetectSourceLanguageConfig with open languages range
            var autoDetectSourceLanguageConfig = AutoDetectSourceLanguageConfig.FromOpenRange();

            // Creates a speech synthesizer with auto detection for source language, using the default speaker as audio output.
            using (var synthesizer = new SpeechSynthesizer(config, autoDetectSourceLanguageConfig,
                                                           AudioConfig.FromDefaultSpeakerOutput()))
            {
                while (true)
                {
                    // Receives a multi lingual text from console input and synthesize it to speaker.
                    // For example, you can input "Bonjour le monde. Hello world.", then you will hear "Bonjour le monde."
                    // spoken in a French voice and "Hello world." in an English voice.
                    Console.WriteLine("Enter some text that you want to speak, or enter empty text to exit.");
                    Console.Write("> ");
                    string text = Console.ReadLine();
                    if (string.IsNullOrEmpty(text))
                    {
                        break;
                    }

                    using (var result = await synthesizer.SpeakTextAsync(text))
                    {
                        if (result.Reason == ResultReason.SynthesizingAudioCompleted)
                        {
                            Console.WriteLine($"Speech synthesized to speaker for text [{text}]");
                        }
                        else if (result.Reason == ResultReason.Canceled)
                        {
                            var cancellation = SpeechSynthesisCancellationDetails.FromResult(result);
                            Console.WriteLine($"CANCELED: Reason={cancellation.Reason}");

                            if (cancellation.Reason == CancellationReason.Error)
                            {
                                Console.WriteLine($"CANCELED: ErrorCode={cancellation.ErrorCode}");
                                Console.WriteLine($"CANCELED: ErrorDetails=[{cancellation.ErrorDetails}]");
                                Console.WriteLine($"CANCELED: Did you update the subscription info?");
                            }
                        }
                    }
                }
            }
        }