.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> Windows Application >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Text to Speech Application

Posted By:Mohammad Elsheimy       Posted Date: February 12, 2011    Points: 75    Category: Windows Application    URL: http://www.dotnetspark.com  

Text to Speech Application in C#, VB.Net. In this article we are going to explore the Speech API library that's part of the TTS SDK that helps you reading text and speaking it. We're going to see how to do it programmatically using C# and VB.NET and how to make use of LINQ to make it more interesting. The last part of this article talks about.. won't tell you more, let's see!
 

Overview

In this article we are going to explore the Speech API library that's part of the TTS SDK that helps you reading text and speaking it. We're going to see how to do it programmatically using C# and VB.NET and how to make use of LINQ to make it more interesting. The last part of this article talks about it ,won't tell you more, let's see!

Introduction

The Speech API library that we are going to use today is represented by the file sapi.dllwhich's located in %windir%\System32\Speech\Common. This library is not part of the .NET BCL and it's not even a .NET library, so we'll use Interoperability to communicate with it (don't worry, using Visual Studio it's just a matter of adding a reference to the application.)

Implementation

In this example, we are going to create a Console application that reads text from the user and speaks it. To complete this example, follow these steps:

As an example, we'll create a simple application that reads user inputs and speaks it. Follow these steps:

  1. Create a new Console application.
     
  2. Add a reference to the Microsoft Speech Object Library (see figure 1.)

    1.JPG
     
  3. Write the following code and run your application:
     
    1. // C#
      using SpeechLib;
      static void Main()
      {
        Console.WriteLine("Enter the text to read:");
        string txt = Console.ReadLine();
        Speak(txt);
      } static void Speak(string text)
      {
        SpVoice voice = new SpVoiceClass();
        voice.Speak(text, SpeechVoiceSpeakFlags.SVSFDefault);
      }

      ' VB.NET
      Imports SpeechLib
      Sub Main()
         Console.WriteLine("Enter the text to read:")
         Dim txt As String = Console.ReadLine()
         Speak(txt)
      End Sub
      Sub Speak(ByVal text As String)
         Dim voice As New SpVoiceClass()
         voice.Speak(text, SpeechVoiceSpeakFlags.SVSFDefault)
      End Sub

If you are using Visual Studio 2010 and .NET 4.0 and the application failed to run because of Interop problems, try disabling Interop Type Embedding feature from the properties on the reference SpeechLib.dll. Building Talking Strings

Next, we'll make small modifications to the code above to provide an easy way to speak a given System.String. 
We'll make use of the Extension Methods feature of LINQ to add the Speak() method created earlier to the System.String.
Try the following code:

 // C#

using SpeechLib;
static void Main()
{
  Console.WriteLine("Enter the text to read:");
  string txt = Console.ReadLine();
  txt.Speak();
}

static void Speak(this string text)
{
  SpVoice voice = new SpVoiceClass();
  voice.Speak(text, SpeechVoiceSpeakFlags.SVSFDefault);
}

' VB.NET
Imports SpeechLib
Imports System.Runtime.CompilerServices
Sub Main()
  Console.WriteLine("Enter the text to read:")
  Dim txt As String = Console.ReadLine()
  txt.Speak()
End Sub

 _ Sub Speak(ByVal text As String)
  Dim voice As New SpVoiceClass()
  voice.Speak(text, SpeechVoiceSpeakFlags.SVSFDefault)
End Sub

I Love You

Let's make it more interesting. We are going to code a VBScript file that says "I Love YOU" when you call it. To complete this example, these steps:
  1. Open Notepad..
  2. CreateObject("SAPI.SpVoice").Speak "I love YOU!"

    Of course, CreateObject() is used to create a new instance of an object resides in a given library. SAPI is the name of the Speech API library stored in Windows Registry. SpVoice is the class name.
  3. Save the file as 'love.vbs' (you can use any name you like, just preserve the vbs extension.)
  4. Now open the file and listen, who is telling that he loves you!!
 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend