patterncsharpModerate
Speech Synthesis to showcase how various voices sound with System.Speech.Synthesis
Viewed 0 times
speechwithsystemsynthesisvoicesshowcasehowsoundvarious
Problem
I was wondering if you would be willing to give me some suggestions on shortening this code. I feel as if the amount of if statements I have is a bit much.
```
using System;
using System.Collections.Generic;
using System.Linq;
using System.Speech.AudioFormat;
using System.Speech.Synthesis;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace SpeechTutorial
{
///
/// Interaction logic for MainWindow.xaml
///
public partial class MainWindow : Window
{
SpeechSynthesizer synth = new SpeechSynthesizer();
public MainWindow()
{
InitializeComponent();
comboItems();
}
private void comboItems()
{
foreach (InstalledVoice voice in synth.GetInstalledVoices())
{
// loop through each voice installed on machine
VoiceInfo info = voice.VoiceInfo;
// adds each installed voice's Name and location to the combobox's list
comboBox.Items.Add(info.Description);
}
}
private void button_Click(object sender, RoutedEventArgs e)
{
// set the synthesizer to speak
synth.Speak(textBox.Text);
}
private void comboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// here we are setting the voices for each selected item in the combobox
try
{
if (comboBox.SelectedIndex == 0)
{
synth.SelectVoice("Microsoft Hazel Desktop");
}
if (comboBox.SelectedIndex == 1)
{
synth.Se
```
using System;
using System.Collections.Generic;
using System.Linq;
using System.Speech.AudioFormat;
using System.Speech.Synthesis;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace SpeechTutorial
{
///
/// Interaction logic for MainWindow.xaml
///
public partial class MainWindow : Window
{
SpeechSynthesizer synth = new SpeechSynthesizer();
public MainWindow()
{
InitializeComponent();
comboItems();
}
private void comboItems()
{
foreach (InstalledVoice voice in synth.GetInstalledVoices())
{
// loop through each voice installed on machine
VoiceInfo info = voice.VoiceInfo;
// adds each installed voice's Name and location to the combobox's list
comboBox.Items.Add(info.Description);
}
}
private void button_Click(object sender, RoutedEventArgs e)
{
// set the synthesizer to speak
synth.Speak(textBox.Text);
}
private void comboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// here we are setting the voices for each selected item in the combobox
try
{
if (comboBox.SelectedIndex == 0)
{
synth.SelectVoice("Microsoft Hazel Desktop");
}
if (comboBox.SelectedIndex == 1)
{
synth.Se
Solution
Take a look at arrays. It will allow you to quickly add and remove future options without needing to change the logic of the code. It will also allow you to possibly add more complex logic in a single place, instead of having to add code to every if/switch statements.
string[] options = { "Microsoft Hazel Desktop",
"Microsoft Heera Desktop",
"Microsoft David Desktop",
"Microsoft Zira Desktop",
"Microsoft Haruka Desktop",
"Microsoft Heami Desktop",
"Microsoft Huihui Desktop",
"Microsoft Tracy Desktop",
"Microsoft Hanhan Desktop"};
try
{
synth.SelectVoice(options[comboBox.SelectedIndex]);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}Code Snippets
string[] options = { "Microsoft Hazel Desktop",
"Microsoft Heera Desktop",
"Microsoft David Desktop",
"Microsoft Zira Desktop",
"Microsoft Haruka Desktop",
"Microsoft Heami Desktop",
"Microsoft Huihui Desktop",
"Microsoft Tracy Desktop",
"Microsoft Hanhan Desktop"};
try
{
synth.SelectVoice(options[comboBox.SelectedIndex]);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}Context
StackExchange Code Review Q#92938, answer score: 11
Revisions (0)
No revisions yet.