private static async Task Synthesize(string text, SpeechSynthesizer synthesizer)
        {
            using (var r = await synthesizer.SpeakTextAsync(text))
            {
                if (r.Reason == ResultReason.SynthesizingAudioCompleted)
                {
                    Console.WriteLine($"Speech synthesized " +
                                      $"to speaker for text [{text}]");
                }
                else if (r.Reason == ResultReason.Canceled)
                {
                    var cancellation = SpeechSynthesisCancellationDetails.FromResult(r);
                    Console.WriteLine($"CANCELED: " +
                                      $"Reason={cancellation.Reason}");

                    if (cancellation.Reason == CancellationReason.Error)
                    {
                        Console.WriteLine($"Cancelled with " +
                                          $"Error Code {cancellation.ErrorCode}");
                        Console.WriteLine($"Cancelled with " +
                                          $"Error Details " +
                                          $"[{cancellation.ErrorDetails}]");
                    }
                }
            }

            Console.WriteLine("Waiting to play " +
                              "back to the audio...");
            Console.ReadKey();
        }
Esempio n. 2
0
        private async void SayItButtonClick(object sender, RoutedEventArgs e)
        {
            try // Basic error handling
            {
                // Check that string is not null or empty
                if (!string.IsNullOrEmpty(PhraseTextBox.Text))
                {
                    // Call SpeakTextAsync and await
                    await _speechSynthesizer.SpeakTextAsync(PhraseTextBox.Text);

                    MessageBox.Show("Text read: '" + PhraseTextBox.Text + "'");
                }
                else // if string is null or empty, display error message
                {
                    MessageBox.Show("Phrase text is required.",
                                    "Error",
                                    MessageBoxButton.OK);
                }
            }
            catch (Exception ex) // Catch exceptions and display in MessageBox
            {
                MessageBox.Show(ex.Message,
                                "Exception",
                                MessageBoxButton.OK);
            }
        }
Esempio n. 3
0
        private async Task SayText(string text)
        {
            try // error handling
            {
                // Stop playing any Synthesized speech
                _speechSynthesizer.CancelAll();

                // Check that string is not null or empty
                if (!string.IsNullOrEmpty(text))
                {
                    // Call SpeakTextAsync and await
                    await _speechSynthesizer.SpeakTextAsync(text);
                }
                else // if string is null or empty, display error message
                {
                    MessageBox.Show("Phrase text is required.",
                                    "Error",
                                    MessageBoxButton.OK);
                }
            }
            catch (Exception ex) // Catch exception and call function to handle it.
            {
                MessageBox.Show(ex.Message);
            }
        }
Esempio n. 4
0
        private async void SetVoiceFromCommand(string voice, string action)
        {
            foreach (var voiceInformation in _availableVoices)
            {
                if (voiceInformation.DisplayName.Split(' ')[1].ToLower() == voice.ToLower())
                {
                    var voiceInfo = voiceInformation;
                    var language  = InstalledLanguages.Single(l => l.Name == voiceInfo.Language);

                    LanguagesListBox.SelectedItem = language;

                    VoicesListBox.SelectedItem = voiceInfo;

                    await _speechSynthesizer.SpeakTextAsync("Voice set to " + voice);

                    if (action == "read")
                    {
                        await SayText();
                    }
                    break;
                }
            }
        }
        private async Task AskForColor()
        {
            try
            {
                SpeechRecognizerUI speechRecognizerUi = new SpeechRecognizerUI();
                SpeechRecognizer   speechRecognizer   = speechRecognizerUi.Recognizer;
                speechRecognizer.AudioCaptureStateChanged += speechRecognizer_AudioCaptureStateChanged;
                speechRecognizer.Grammars.AddGrammarFromList("colorList", _colorNames);

                PromptTextBlock.Text  = "Which color?";
                ExampleTextBlock.Text = "'Red', 'Blue', 'Green', 'Yellow', 'Purple', 'Orange', 'Black', 'White'";
                _speechSynthesizer.SpeakTextAsync("Which color?");

                SpeechRecognitionResult result = await speechRecognizer.RecognizeAsync();

                if (result.TextConfidence < SpeechRecognitionConfidence.Medium)
                {
                    FillUi(result);
                    Dispatcher.BeginInvoke(() => PromptTextBlock.Text = "Recognition Confidence too low.");
                    _speechSynthesizer.SpeakTextAsync("Recognition Confidence too low. Please try again.");
                    await AskForColor();
                }
                else
                {
                    SetColorFromCommand(result.Text);
                    PromptTextBlock.Text = "Color set to " + result.Text;
                    FillUi(result);
                    _speechSynthesizer.SpeakTextAsync("Color set to " + result.Text);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                //throw;
            }
        }
Esempio n. 6
0
        //
        public static async Task SynthesisToSpeakerAsync()
        {
            // 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").
            var config = SpeechConfig.FromSubscription("b7a2c092a3bf4adc99432e737c97ff7f", "westus");

            // Creates a speech synthesizer using the default speaker as audio output.
            using (var synthesizer = new SpeechSynthesizer(config))
            {
                // Receive a text from console input and synthesize it to speaker.
                string choose = "";
                if (Global.pass_recognize == "Hello.")
                {
                    choose = "Hello nice to meet you";
                }
                else
                {
                    choose = "I don't understand";
                }
                string text = choose;

                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?");
                        }
                    }
                }
            }
        }
Esempio n. 7
0
        private async void SayItButtonClick(object sender, RoutedEventArgs e)
        {
            try // Basic error handling
            {
                // Disable the ListBoxes to prevent exceptions
                VoicesListBox.IsEnabled    = false;
                LanguagesListBox.IsEnabled = false;

                // Stop playing any Synthesized speech
                _speechSynthesizer.CancelAll();

                // Check that string is not null or empty
                if (!string.IsNullOrEmpty(PhraseTextBox.Text))
                {
                    // Call SpeakTextAsync and await
                    await _speechSynthesizer.SpeakTextAsync(PhraseTextBox.Text);

                    MessageBox.Show("Text read: '" + PhraseTextBox.Text + "'");
                }
                else // if string is null or empty, display error message
                {
                    MessageBox.Show("Phrase text is required.",
                                    "Error",
                                    MessageBoxButton.OK);
                }

                // Re-enable the ListBoxes
                VoicesListBox.IsEnabled    = true;
                LanguagesListBox.IsEnabled = true;
            }
            catch (Exception ex) // Catch exceptions and display in MessageBox
            {
                MessageBox.Show(ex.Message,
                                "Exception",
                                MessageBoxButton.OK);
            }
        }