Building Applications that Can Talk in VB.NET

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 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.)

    Add reference to Microsoft Speech Object Library.jpg
     
  3. Write the following code and run your application:

    ' 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 StringsNext, 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:
' 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<Extension()> _
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. Write the following code: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!
Microsoft Speech API has many voices; two of them are Microsoft Sam (male), the default for Windows XP and Windows 2000, and Microsoft Ann (female), the default 
for Windows Vista and Windows 7. Read more about Microsoft TTS voices here.
Thanks to our friend, Mohamed Gafar, for providing the VBScript.


Similar Articles