Convert Text to Speech Using ASP.Net C#

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.
Download Aspose : API To Create and Convert Files
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.
  
<%@ Page  Async="true" Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ConvertTextToSpeech.Default" %>  
  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  
<html xmlns="http://www.w3.org/1999/xhtml">  
  
<head>  
     
        <title>Article by Vithal Wadje</title>    
    </head>    
    <body bgcolor="navy">    
        <form id="form2" runat="server">    
        <div style="color: White;">    
            <h4>    
                Article for C#Corner    
            </h4>    
            <br />    
            <table width="100%">    
                <tr>    
                    <td>    
                        <asp:TextBox ID="txtMsg" TextMode="MultiLine" runat="server" Height="142px" Width="380px"></asp:TextBox><br />    
                    </td>    
                </tr>    
            </table>    
            <br />    
             <table>    
                <tr>    
                      
                    <td>    
                          <asp:Button ID="btVoice" runat="server"   
                            Text="convert to speech" onclick="btnVoice_Click" />    
                    </td>    
                    
                </tr>    
            </table>    
              
        </div>    
        </form>    
    </body>    
    </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:
protected void btnVoice_Click(object sender, EventArgs e)  
         {  
      
             // creating the object of SpeechSynthesizer class  
             SpeechSynthesizer sp = new SpeechSynthesizer();  
             //setting volume   
             sp.Volume = 100;  
             //passing text box text to SpeakAsync method   
             sp.SpeakAsync(txtMsg.Text);  
            
         } 
 the entire default.aspx.cs code will look as follows:
using System;  
    using System.Speech.Synthesis;  
    namespace ConvertTextToSpeech  
    {  
        public partial class Default : System.Web.UI.Page  
        {  
             
            protected void Page_Load(object sender, EventArgs e)  
            {  
      
            }  
      
            protected void btnVoice_Click(object sender, EventArgs e)  
            {  
      
                // creating the object of SpeechSynthesizer class  
                SpeechSynthesizer sp = new SpeechSynthesizer();  
                //setting volume   
                sp.Volume = 100;  
                //passing text box text to SpeakAsync method   
                sp.SpeakAsync(txtMsg.Text);  
               
            }  
      
        }  
    } 
 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.
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.



Post a Comment

www.CodeNirvana.in

Protected by Copyscape Online Copyright Protection
Copyright © Compile Mode