Exemple #1
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());
        }
Exemple #2
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?");
                            }
                        }
                    }
                }
            }
        }
        public static async Task RecognizeLng()
        {
            SpeechConfig speechConfig = SpeechConfig.FromEndpoint(new System.Uri(ConfigurationManager.AppSettings.Get("SpeechEndpoint")), ConfigurationManager.AppSettings.Get("TTSKey"));
            AudioConfig  audioConfig  = AudioConfig.FromDefaultSpeakerOutput();
            AutoDetectSourceLanguageConfig autoDetectSourceLanguageConfig = AutoDetectSourceLanguageConfig
                                                                            .FromLanguages(new string[] { "en-US", "ru-RU" });

            using (var recognizer = new SpeechRecognizer(
                       speechConfig,
                       autoDetectSourceLanguageConfig,
                       audioConfig))
            {
                Console.WriteLine("Say something...");
                var speechRecognitionResult = await recognizer.RecognizeOnceAsync();

                var autoDetectSourceLanguageResult =
                    AutoDetectSourceLanguageResult.FromResult(speechRecognitionResult);
                var detectedLng = autoDetectSourceLanguageResult.Language;
                Console.WriteLine("I recognized " + speechRecognitionResult.Text + " in " + detectedLng);
            }
        }
Exemple #4
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);
        }
Exemple #5
0
        // https://docs.microsoft.com/en-us/azure/cognitive-services/speech-service/how-to-recognize-speech-csharp

        public void Start(string culture, Action <string> recognized, string wakeupKeyword = default, int awakeTimeout = 6000, Action <bool> awakeningHandler = default, Action <string> recognizing = default)
        {
            this.wakeupKeyword    = wakeupKeyword;
            this.awakeTimeout     = awakeTimeout;
            this.awakeningHandler = awakeningHandler;

            var audioInput        = AudioConfig.FromDefaultMicrophoneInput();
            var speechInputConfig = SpeechConfig.FromSubscription(config["SpeechToText.SubscriptionKey"], config["SpeechToText.SpeechRegion"]);

            speechInputConfig.OutputFormat = OutputFormat.Detailed;
            speechInputConfig.SpeechRecognitionLanguage = culture;
            recognizer = new SpeechRecognizer(speechInputConfig, audioInput);

            recognizer.Recognizing += (s, e) =>
            {
                isRecognizing = true;
                if (recognizing == default)
                {
                    return;
                }
                var text = e.Result.Text.Clean();
                if (text.IsEmpty())
                {
                    return;
                }
                recognizing(text);
            };

            recognizer.Recognized += (s, e) => {
                isRecognizing = false;
                if (e.Result.Reason != ResultReason.RecognizedSpeech)
                {
                    return;
                }
                var text = e.Result.Text.Clean();

                if (text.IsEmpty())
                {
                    return;
                }
                if (this.awakening != null)
                {
                    recognized(text);
                    WakeDown();
                }
                else
                {
                    var wakeupKeywordIndex = text.IndexOf(wakeupKeyword, 0, StringComparison.InvariantCultureIgnoreCase);
                    if (wakeupKeywordIndex < 0)
                    {
                        return;
                    }

                    var beforeText = string.Empty;
                    if (wakeupKeywordIndex > 0)
                    {
                        beforeText = text.Substring(0, wakeupKeywordIndex - 1).Clean();
                    }

                    var afterText = text.Substring(wakeupKeywordIndex + wakeupKeyword.Length).Clean();
                    if (afterText.IsEmpty())
                    {
                        if (beforeText.IsEmpty())
                        {
                            WakeUp();
                        }
                        else
                        {
                            recognized(beforeText);
                        }
                    }
                    else if (beforeText.IsEmpty())
                    {
                        recognized(afterText);
                    }
                    else
                    {
                        recognized($"{beforeText} {afterText}");
                    }
                }
            };

            recognizer.StartContinuousRecognitionAsync().Wait();

            var audioOutput        = AudioConfig.FromDefaultSpeakerOutput();
            var speechOutputConfig = SpeechConfig.FromSubscription(config["SpeechToText.SubscriptionKey"], config["SpeechToText.SpeechRegion"]);

            speechOutputConfig.SpeechSynthesisLanguage  = culture;
            speechOutputConfig.SpeechSynthesisVoiceName = config[$"TextToSpeech.VoiceName.{culture}"];
            syntetizer = new SpeechSynthesizer(speechOutputConfig, audioOutput);
        }