Click here to Skip to main content
15,879,474 members
Articles / Programming Languages / C#
Tip/Trick

Converting Text to Voice in .NET Using C#

Rate me:
Please Sign up or sign in to vote.
4.80/5 (31 votes)
11 Nov 2013Apache2 min read 74.9K   5.4K   35   10
How to convert text to voice in .NET using C#

Introduction

The demo below explains how to convert text to speech using C# in Visual Studio 2010 using System.Speech Library.

Microsoft .NET framework provides <a href="http://msdn.microsoft.com/en-US/library/System.Speech.Synthesis.aspx">System.Speech.Synthesis</a> for voice synthesis.

Building the Sample

You must have Visual Studio 2010 to build and run the sample.

Description

To convert text to speech which is called voice synthesis, you must include "System.Speech" reference in your project. It contains functions for both speech synthesis and recognition.

After adding System.speech reference, you have to use System.Speech.Synthesis in your project which contains various functions for voice synthesis.

Creating Object of SpeechSynthesizer Class

The first step is to create an object of SpeechSynthesizer class (e.g. "reader" in my sample).

C#
SpeechSynthesizer reader = new SpeechSynthesizer();

Calling Speak() Function

The next step is to call the "Speak()" function by passing the text to spoken as string.

C#
reader.Speak("This is my first speech project");

The problem with the speak() function is that it is not threaded. It means that you cannot perform any other function in your Windows Form until the "reader" object has completed the speech.

So it is better to use "SpeakAsync()" function. I have also used SpeakAsync() in the sample.

C#
reader.SpeakAsync("Speaking text asynchronously");

Using Pause() and Resume() functions

You can also detect the state of the "reader" object by using "SynthesizerState" property. And using that, you can also "Pause" or "Resume" the narration.

C#
 if (reader.State == SynthesizerState.Speaking)
   {
                    reader.Pause();
   }

if (reader.State == SynthesizerState.Paused)
   {
                    reader.Resume();
  }

Using Dispose() function

You can use Dispose() function to stop narration and dispose the "reader" object.

C#
if( reader!= null )
{
     reader.Dispose();
}

These are some of the basic operations. You can also change voice, volume, rate and other parameters. You can also save the spoken audio stream directly into a "wave" file.

The sample also shows some other features such as using event handlers to detect speech progress and display status of synthesizer.

Below is the complete code for speech synthesis.

COMMENTS and QUESTIONS are most welcome......HAPPY CODING!!!!

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Speech.Synthesis;
using System.IO;

namespace text_to_speech
{
    public partial class Form1 : Form
    {
        SpeechSynthesizer reader; //declare the object
        public Form1()
        {
            InitializeComponent();

        }
        private void Form1_Load(object sender, EventArgs e)
        {
            reader = new SpeechSynthesizer(); //create new object
            button2.Enabled = false;
            button3.Enabled = false;
            button4.Enabled = false;
            textBox1.ScrollBars = ScrollBars.Both;
        }

        //SPEAK TEXT
        private void button1_Click(object sender, EventArgs e)
        {
            reader.Dispose();
            if (textBox1.Text != "")    //if text area is not empty
            {

                reader = new SpeechSynthesizer();
                reader.SpeakAsync(textBox1.Text);
                label2.Text = "SPEAKING";
                button2.Enabled = true;
                button4.Enabled = true;
                reader.SpeakCompleted += new EventHandler<speakcompletedeventargs />(reader_SpeakCompleted);
            }
            else
            {
                MessageBox.Show("Please enter some text in the textbox",
                 "Message", MessageBoxButtons.OK);
            }
        }

        //event handler
        void reader_SpeakCompleted(object sender, SpeakCompletedEventArgs e)
        {
            label2.Text = "IDLE";
        }

        //PAUSE
        private void button2_Click(object sender, EventArgs e)
        {
            if (reader != null)
            {
                if (reader.State == SynthesizerState.Speaking)
                {
                    reader.Pause();
                    label2.Text = "PAUSED";
                    button3.Enabled = true;

                }
            }
        }

        //RESUME
        private void button3_Click(object sender, EventArgs e)
        {
            if (reader != null)
            {
                if (reader.State == SynthesizerState.Paused)
                {
                    reader.Resume();
                    label2.Text = "SPEAKING";
                }
                button3.Enabled = false;
            }
        }

        //STOP
        private void button4_Click(object sender, EventArgs e)
        {
            if (reader != null)
            {
                reader.Dispose();
                label2.Text = "IDLE";
                button2.Enabled = false;
                button3.Enabled = false;
                button4.Enabled = false;
            }
        }

        //load data from text file button
        private void button5_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
        }

        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {
            //copy data from text file and load it to textbox
            textBox1.Text =  File.ReadAllText(openFileDialog1.FileName.ToString());

        }

     }
}

Adding SYSTEM.SPEECH Reference

Image 1

Image 2

License

This article, along with any associated source code and files, is licensed under The Apache License, Version 2.0


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionothers Languages Pin
Member 120424517-Oct-15 23:11
Member 120424517-Oct-15 23:11 
GeneralReview after implementing Pin
Member 1139899127-Feb-15 20:40
Member 1139899127-Feb-15 20:40 
QuestionPlease HELP Pin
ProjectShift19-Dec-13 13:08
ProjectShift19-Dec-13 13:08 
QuestionThank you. Pin
WuRunZhe13-Nov-13 14:45
WuRunZhe13-Nov-13 14:45 
Suggestionappreciation Pin
Member 1039894913-Nov-13 1:56
Member 1039894913-Nov-13 1:56 
GeneralMy vote of 5 Pin
JayantaChatterjee12-Nov-13 22:25
professionalJayantaChatterjee12-Nov-13 22:25 
GeneralMy vote of 5 Pin
Champion Chen12-Nov-13 14:48
Champion Chen12-Nov-13 14:48 
very fun~ Thanks !
QuestionDifferent languages Pin
balesteros11-Nov-13 22:27
professionalbalesteros11-Nov-13 22:27 
GeneralMy vote of 5 Pin
URVISH_SUTHAR111-Nov-13 22:08
URVISH_SUTHAR111-Nov-13 22:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.