Introduction
Text To Speech becomes very easy in C#. We know speech technology is very useful for the blind. Now, we are going to learn how to implement speech technology in our project.
STEPS
- Open Microsoft Visual Studio 2010 from Start | All Programs | Microsoft Visual Studio 2010 | Microsoft Visual Studio 2010.
- On the Microsoft Visual Studio Start Page, click the New Project icon.
- On the Microsoft Visual Studio Start Page, click the New Project icon.
- In C# projects Templates list, select Windows | WindowsApplication.
- In the Name field, enter the name “text to speak”
- In the upper right hand corner, ensure that version 4.0 of the .NET Framework is selected.
- Accept the default location and Solution Name. Click the OK button. The solution will be created.
- Right-click on the Text to Speak project node and select Add Reference.
- Under the .NET tab in the Add Reference dialog box, select System.Speech.
- Click the OK button.
Select a Text File
Select a text file to convert audio.selected
text file display on rich text box.
private void Browse_Click(object sender, EventArgs e)
{
OpenFileDialog Ofg = new OpenFileDialog();
try
{
Ofg.CheckFileExists = true;
Ofg.CheckPathExists = true;
Ofg.DefaultExt = "txt";
Ofg.DereferenceLinks = true;
Ofg.Filter = "Text files (*.txt)|*.txt|" +
"RTF files (*.rtf)|*.rtf|" +
" +
Works 6 and 7 (*.wps)|*.wps|" +
"Windows Write (*.wri)|*.wri|" +
"WordPerfect document (*.wpd)|*.wpd";
Ofg.Multiselect = false;
Ofg.RestoreDirectory = true;
Ofg.ShowHelp = true;
Ofg.ShowReadOnly = false;
Ofg.Title = "Select a file ";
OpenFile.ValidateNames = true;
if (OpenFile.ShowDialog() == DialogResult.OK)
{
StreamReader sr = new StreamReader(ofg.OpenFile());
richTextBox1.Text = sr.ReadToEnd();
}
}
catch
{
MessageBox.Show("can not open the file", "Text two speech");
}
}
Here, first creating object instance for OpenFileDialog.stream
reader is used to read a text file.
Text to Speak
SpeakAsync
method is used to speak a word. There are two properties used here; one is volume
, another one is Rate
. voice
gender is also one of the properties. It is used to select voice
gender (enum NotSet
, Male
, Female
, Neutral
). If any error occurred, message box shows error message.
private void speak_Click(object sender, EventArgs e)
{
SpeechSynthesizer speak = new SpeechSynthesizer();
try
{
switch (comboBox1.SelectedItem.ToString())
{
case "NotSet":
voice.SelectVoiceByHints(VoiceGender.NotSet);
break;
case "Male":
voice.SelectVoiceByHints(VoiceGender.Male);
break;
case "Female":
voice.SelectVoiceByHints(VoiceGender.Female);
break;
case "Neturl":
voice.SelectVoiceByHints(VoiceGender.Neutral);
break;
}
The above code can change voice
gender. speakAsyn
is an asynchronous method. It means return immediately and speak as a background process. If any error occurred, it will display on message box.
voice.Volume = trackBar1.Value;
voice.Rate = trackBar2.Value;
voice.SpeakAsync(richTextBox1.Text);
Save Audio File
Text converted to audio stream .audio
stream can save wav file format. The following steps are used to save wav file.
- Create object instance for Save File Dialog.
- Set audio stream to select file.
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = ""wav files (*.wav)|*.wav";
sfd.Title = "Save to a wave file";
sfd.FilterIndex = 2;
sfd.RestoreDirectory = true;
if (sfd.ShowDialog() == DialogResult.OK)
{
FileStream fs = new FileStream(sfd.FileName,FileMode.Create,FileAccess.Write);
voice.SetOutputToWaveStream(fs);
voice.Speak(richTextBox1.Text);
fs.Close();
}
Points of Interest
If you want to know more about speech technology, click on this link on the Microsoft website.
History
- 14th April, 2011: Initial version