Convert Text to Speech Using ASP.Net C#

Background
 
Day by day business needs change depending on business requirements and fulfilling these requirements BPO and KPO come that require a physical assistance of humans to support specific requirements but some requirements exist in the application to convert text into speech without a human involved. So by considering the preceding requirement I decided to write this article.

To convert text to speech the SpeechSynthesizer class is used. So let us learn step-by-step how to convert text to speech.
 
The SpeechSynthesizer Class
 
SpeechSynthesizer is a sealed class containing various methods, events and properties that help to convert text to speech with various options. The SpeechSynthesizer class is in the System.Speech.Synthesis namespace that contains classes that allow initialization and configuration of a speech synthesis engine, create prompts, generate speech, respond to events and modify voice characteristics. Speech synthesis is often referred to as Text-To-Speech or TTS.
 
The following are some of the common properties of the SpeechSynthesizer class.
 
Properties of SpeechSynthesizer class 
  • Rate: Used to specify speech rate.
  • State: Used to check the current state of the SpeechSynthesizer class, whether it is paused, resume or a speaking state.
  • Voice: Gets the current voice information.
  • Volume: Sets the volume.

Events of SpeechSynthesizer class 

  • BookmarkReached.
  • PhonemeReached.
  • SpeakCompleted.
  • SpeakProgress.
  • SpeakStarted.
  • StateChanged.
  • VisemeReached.
  • VoiceChange

Methods of SpeechSynthesizer class  

  • AddLexicon    
  • Dispose   
  • GetCurrentlySpokenPrompt
  • GetInstalledVoices
  • GetInstalledVoices
  • Pause
  • RemoveLexicon
  • Resume
  • SelectVoice
  • SelectVoiceByHints
  • SetOutputToDefaultAudioDevice
  • SetOutputToNull
  • SetOutputToWaveFile
  • SetOutputToWaveStream
  • Speak
  • SpeakAsync
  • SpeakAsyncCancel
  • SpeakAsyncCancelAll
  • SpeakSsml
  • SpeakSsmlAsync

Now let us see the preceding explanation by creating a sample web application as follows:

  1. "Start" - "All Programs" - "Microsoft Visual Studio 2010".

  2. "File" - "New Project" - "C#" - "Empty WebSite" (to avoid adding a master page).

  3. Provide the web site a name such as "TextToSppech" or another as you wish and specify the location.

  4. Then right-click on the Solution Explorer and select "Add New Item" and Add Web Form.

  5. Drag and drop one Button, a TextBox  onto the <form> section of the Default.aspx page.

Now the default.aspx page source code will looks as follows.

  1. <%@ Page  Async="true" Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ConvertTextToSpeech.Default" %>  
  2.   
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6.   
  7. <head>  
  8.      
  9.         <title>Article by Vithal Wadje</title>    
  10.     </head>    
  11.     <body bgcolor="navy">    
  12.         <form id="form2" runat="server">    
  13.         <div style="color: White;">    
  14.             <h4>    
  15.                 Article for C#Corner    
  16.             </h4>    
  17.             <br />    
  18.             <table width="100%">    
  19.                 <tr>    
  20.                     <td>    
  21.                         <asp:TextBox ID="txtMsg" TextMode="MultiLine" runat="server" Height="142px" Width="380px"></asp:TextBox><br />    
  22.                     </td>    
  23.                 </tr>    
  24.             </table>    
  25.             <br />    
  26.              <table>    
  27.                 <tr>    
  28.                       
  29.                     <td>    
  30.                           <asp:Button ID="btVoice" runat="server"   
  31.                             Text="convert to speech" onclick="btnVoice_Click" />    
  32.                     </td>    
  33.                     
  34.                 </tr>    
  35.             </table>    
  36.               
  37.         </div>    
  38.         </form>    
  39.     </body>    
  40.     </html>   
 Now add the reference of system.speech by right-clicking on the Solution Explorer as in the following:
 
 
 
After adding the reference the Solution Explorer will look as follows:
 
 
 
 
Now double-click on the convert to speech button and write the following code:
  1. protected void btnVoice_Click(object sender, EventArgs e)  
  2.      {  
  3.   
  4.          // creating the object of SpeechSynthesizer class  
  5.          SpeechSynthesizer sp = new SpeechSynthesizer();  
  6.          //setting volume   
  7.          sp.Volume = 100;  
  8.          //ing text box text to SpeakAsync method   
  9.          sp.SpeakAsync(txtMsg.Text);  
  10.         
  11.      } 
 the entire default.aspx.cs code will look as follows:
  1. using System;  
  2. using System.Speech.Synthesis;  
  3. namespace ConvertTextToSpeech  
  4. {  
  5.     public partial class Default : System.Web.UI.Page  
  6.     {  
  7.          
  8.         protected void Page_Load(object sender, EventArgs e)  
  9.         {  
  10.   
  11.         }  
  12.   
  13.         protected void btnVoice_Click(object sender, EventArgs e)  
  14.         {  
  15.   
  16.             // creating the object of SpeechSynthesizer class  
  17.             SpeechSynthesizer sp = new SpeechSynthesizer();  
  18.             //setting volume   
  19.             sp.Volume = 100;  
  20.             //ing text box text to SpeakAsync method   
  21.             sp.SpeakAsync(txtMsg.Text);  
  22.            
  23.         }  
  24.   
  25.     }  

 Now run the application. The UI will look such as follows:
 
 
 
Now enter some text in the preceding text box as in the following:
 
 
Now click on the Convert to speech button. It will convert text to speech.
 
Notes
  • Enter proper readable text.
  • Ensure your system sounds are working.
  • For more details and explanation, download the Uploaded Zip file.
Summary

From all the preceding examples you have learned how to convert text to speech. I hope this article is useful for all readers, if you have a suggestion then please contact me.

Up Next
    Ebook Download
    View all
    Learn
    View all