One of the cool things that came out with .NET 3.x is the addition of System.Speech library. This library is a collection of classes that enables you to do alot of speech related things like speech recognition and text-to-speech conversion. It's a handful to talk about so i suggest you read up MSDN to learn more about this library[1][2][3].
The app I'm going to show you today is a basic application that shows how to utilize the SpeechSynthesizer class that is located inside the System.Speech.Synthesis namespace.
To start this demo lets add a reference to the System.Speech namespace to our project. You can do this by doing a right-click on References > Add Reference and selecting System.Speech from the list.
Once added we can now beging utilizing this library by adding a using directive pointing to the specific System.Speech namepace that we want to utilize. For this demo we will use System.Speech.Synthesis
Next, we need to create a new SpeechSynthesizer object. SpeechSynthesizer is class that enables you to convert text-to-speech. The class also has several properties and methods that you can use customize the voice information on your speech synthesizer.
Next is the fun part which is making our application say some words. The cool thing about SpeechSynthersizer is that all you need to do to make your application speak is call the SpeechSynthesizer.Speak() method and your done ;)
Run our application and once started you should here the words "Hello, World" spoken by your machine. Pretty cool huh?!
What we did was a simple demonstration on how to make our apps speak with a few lines using SpeechSynthesizer. But what about customizing the voice? Fear not! I created a sample application that will show you how you can customize the synthesizer by specifying the rate, volume, gender and age of the emitted sound. Below is the screenshot showing the UI for the application
And here is the code snippet with comments detailing how to customize our SpeechSynthesizer object.
using System; using System.Windows.Forms; using System.Speech.Synthesis; namespace KeithRull.TalkToMeGoose { public partial class MainForm : Form { public MainForm() { InitializeComponent(); } //create a new speechsynthesizer object SpeechSynthesizer speechSynth = new 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; string selectedGender = genderComboBox.SelectedItem.ToString(); string selectedAge = ageComboBox.SelectedValue.ToString(); Type voiceGenderType = typeof(VoiceGender); Type voiceAgeType = typeof(VoiceAge); //convert the selectedGender value to a VoiceGender VoiceGender gender = (VoiceGender)Enum.Parse(voiceGenderType, selectedGender); //convert the selectedAge value to a VoiceAge VoiceAge age = (VoiceAge)Enum.Parse(voiceAgeType, selectedAge); //specify the volume for our SpeechSynthesizer object speechSynth.Volume = selectedVolume; //specify the rate for our SpeechSynthesizer object speechSynth.Rate = selectedVoiceRate; //specify the voice info by using hints regarding the gender and age speechSynth.SelectVoiceByHints(gender, age); //say the message speechSynth.SpeakAsync(messageToSay); } private void MainForm_Load(object sender, EventArgs e) { speechSynth.Speak("Hello, World!"); } /// <summary> /// Bind the enums to our combobox /// </summary> private void BindData() { BindAgeToComboBox(); BindGenderToComboBox(); } /// <summary> /// bind the VoiceGender enum to our combobox /// </summary> private void BindGenderToComboBox() { //convert the enumeration to a string array Array voiceGenderArray = Enum.GetValues(typeof(VoiceGender)); //bind the array to the datasource of our combobox genderComboBox.DataSource = voiceGenderArray; } /// <summary> /// Bind the VoiceAge enum to our combobox /// </summary> private void BindAgeToComboBox() { //convert the enumeration to a string array Array voiceAgeArray = Enum.GetValues(typeof(VoiceAge)); //bind the array to the datasource of our combobox ageComboBox.DataSource = voiceAgeArray; } } };
As always, you can download the source code for this project here: KeithRull.TalkToMeGoose1.zip (27.46 KB)
I hope I was able to show you how simple it is to add text to speech functionality to your .NET application. Next time I'll show you how to use predefined voices. Till next time ;)
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.