Text to Speech using C# in Windows Forms Application

In this post I will explain how to convert text to speech using C# in Windows Forms Application

Download Source Code

Introduction

In this post we will see how to convert text to speech (TTS) using a built in Narrator installed in the system. We will use SpeechSynthesizer class of System.Speech library to built a TTS application

In this application, we will add an option to select a voice from installed narrators and an option to play, pause and resume the speech. Audio of this speech can also be downloaded in Wave(.wav) format using a download button

   Text To Speech Using C Sharp

Step 1:

Create a new Windows Forms Application

New Project_2013-01-30_23-25-05

Step 2:

Add reference to System.Speech library using Add Reference dialog box

System.Speech Library

Import following namespace in Form1.cs

using System.Speech.Synthesis;

Step 3:

Add following controls on Form1 and arrange them as shown in the first figure above

Document Outline TTS

Step 4:

Declare following variable under Form1 class declaration

SpeechSynthesizer speaker = new SpeechSynthesizer();
bool flag = false;

SpeechSynthesizer is the class that we will use to make a text speak

Step 5:

Add following code in Form1 constructor

public Form1()
{
     InitializeComponent();
     speaker.SpeakCompleted += new EventHandler<SpeakCompletedEventArgs> (speaker_SpeakCompleted);
     btnPause.Enabled = false;

     foreach (InstalledVoice voice in speaker.GetInstalledVoices())
     {
          cbVoice.Items.Add(voice.VoiceInfo.Name);
     }
}

void speaker_SpeakCompleted(object sender, SpeakCompletedEventArgs e)
{
     btnPlay.Enabled = true;
     btnPause.Enabled = false;
}

GetInstalledVoices method of the SpeechSynthesizer class returns all the voices or narrators installed in the system as a collection of InstalledVoice objects. It is bound then to the ComboBox to enable users to select a voice.

SpeakCompleted event triggered when a SpeechSynthesizer completes speaking. It is just used to enable or disable buttons.

Step 6:

Add following code in the click event of btnPlay

private void btnPlay_Click(object sender, EventArgs e)
{
     if (cbVoice.SelectedIndex >= 0)
     {
          btnPlay.Enabled = false;
          btnPause.Enabled = true;
          Play(txtWords.Text);
     }
     else
     {
          MessageBox.Show("Please select a voice", "Text to Speech",
          MessageBoxButtons.OK, MessageBoxIcon.Warning);
          cbVoice.Focus();
     }
}

private void Play(string words)
{
     speaker.SelectVoice(cbVoice.Text);
     speaker.SpeakAsync(words);
}

Play method is created to convert text send as parameter to speech. Here, SelectVoice of SpeechSynthesizer class is used to select the voice of a narrator. SelectVoiceByHints method can also be used to select a voice by specifying age, gender etc.

SpeakAsync method is used to convert text to speech and plays asynchronously. It takes string as input to speak out.

Step 7:

Add following code in the click event of btnPause

private void btnPause_Click(object sender, EventArgs e)
{
     if (speaker != null)
     {
          if (flag == false)
          {
               speaker.Pause();
               btnPause.Text = "Resume";
               flag = true;
          }
          else
          {
               speaker.Resume();
               btnPause.Text = "Pause";
               flag = false;
          }
     }
}

Pause and Resume methods of SpeechSynthesizer class are used to play and pause the synthesizer respectively.

Step 8:

Add following code in the click event of btnDownload

private void btnDownload_Click(object sender, EventArgs e)
{
     FolderBrowserDialog browser = new FolderBrowserDialog();
     if (browser.ShowDialog() == DialogResult.OK)
     {
          SpeechSynthesizer MySynthesizer = new SpeechSynthesizer();
          MySynthesizer.SpeakCompleted += new EventHandler<SpeakCompletedEventArgs> (MySynthesizer_SpeakCompleted);
          MySynthesizer.SetOutputToWaveFile(string.Concat(browser.SelectedPath, "\MyTTS.wav"));
          PromptBuilder builder = new PromptBuilder();
          builder.AppendText(txtWords.Text);
          MySynthesizer.SpeakAsync(builder);
     }
}

void MySynthesizer_SpeakCompleted(object sender, SpeakCompletedEventArgs e)
{
     MessageBox.Show("Audio downloaded sucessfully", "Text to Speech", MessageBoxButtons.OK, MessageBoxIcon.Information);
}

SetOutputToWaveFile method of SpeechSynthesizer class is used here to output text to a Wave file. It takes file location as parameter. When SpeakAsync is called with PromptBuilder, it writes output of the text given with prompt to a wave(.wav) file.

Thanks for reading!

4 thoughts on “Text to Speech using C# in Windows Forms Application

  1. RAJACK

    I am working on a project in which I have to merge (TEXT TO SPEECH) converted wave file with another wave file in to a single wave file which has to be played later. I am able to merge the wave files but problem is when I play the merged file , (TEXT TO SPEECH)converted wave file get played very fast which is not clear. what may be the problem..? programmatically(c#) how can I make the song to play in the same flow? how can I change the bit rate of merged wave file..? is there anything to do with the wavelength?

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *