In part 1 of this series i showed you how to specify the voice, gender, rate and volume of the our SpeechSynthesizer object. This time i'm going to show you how to use predefined Voices in your machine and utilize it as hints to your SpeechSynthesizer object.
The first thing that I did to our sample project is change the UI since we would not need the Gender and Age combo box in our form. The result is a UI like this:
Next, we need to figure out a way to extract the names of the installed voices in our machine. To this we need to use the SpeechSynthesizer.GetInstalledVoices() method. This method, when invoked returns a readonly collection of TTS(text-to-speech) voices also known as InstalledVoice objects that are readily available in your machine. The InstalledVoice object contains a property called VoiceInfo which represents the voice information about that TTS voice. To begin our project we need to get all the VoiceInfo objects on each and every InstalledVoice. Below is a code snippet showing how we can accomplish this task:
//create a new speechsynthesizer object static SpeechSynthesizer speechSynth = new SpeechSynthesizer();
/// <summary> /// a method that returns all the currently installed voice /// info objects in the machine /// </summary> /// <returns>a list of VoiceInfo objects</returns> private static List<VoiceInfo> GetInstalledVoices() { //get the current cultureinfo CultureInfo currentCulture = CultureInfo.CurrentCulture; //use linq to select each voiceinfo object from the intalledvoices collection var listOfVoiceInfo = from voice in speechSynth.GetInstalledVoices(currentCulture) select voice.VoiceInfo; //return the selected voiceinfo objects return listOfVoiceInfo.ToList<VoiceInfo>(); }
Next, we need to bind the resulting list to our voiceComboBox.
private void MainForm_Load(object sender, EventArgs e) { BindData(); } /// <summary> /// Bind the voices to our combobox /// </summary> private void BindData() { //get the installed voices List<VoiceInfo> listOfVoices = GetInstalledVoices(); //bind the list to our combobox voicesComboBox.DataSource = listOfVoices; voicesComboBox.DisplayMember = "Name"; }
And finally we need to create the logic inside our button click event to initiate our SpeechSynthesizer
private void speakUpButton_Click(object sender, EventArgs e) { //get the values specified in our form string messageToSay = wordsTextBox.Text; int selectedVolume = volumeTrackBar.Value; int selectedVoiceRate = voiceRateTrackBar.Value; //get the selected voice info VoiceInfo vi = voicesComboBox.SelectedItem as VoiceInfo; //specify the volume for our SpeechSynthesizer object speechSynth.Volume = selectedVolume; //specify the rate for our SpeechSynthesizer object speechSynth.Rate = selectedVoiceRate; //specify the voice info specifying the voice to use speechSynth.SelectVoice(vi.Name); //say the message speechSynth.SpeakAsync(messageToSay); }
And we are done ;) Running the application would show as all the installed TTS voice in our machine. One thing to note is that this list is machine dependent and different machines might contain different voices.
Clicking the "Speak!" button should echo our selected voice.
Next time I'll show you how to export the resulting speech to a wave file. HTH
Powered by: newtelligence dasBlog 2.3.9074.18820
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.
E-mail
Theme design by Jelle Druyts