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: Create a new Console application. Add a reference to the Microsoft Speech Object Library (see figure 1.) Write the following code and run your application: // 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 SubIf 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.NETImports SpeechLib Imports System.Runtime.CompilerServicesSub 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: Open Notepad..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. Save the file as 'love.vbs' (you can use any name you like, just preserve the vbs extension.) Now open the file and listen, who is telling that he loves you!!
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
Building Talking Strings
Next, we'll make small modifications to the code above to provide an easy way to speak a given System.String.
// 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.NETImports SpeechLib Imports System.Runtime.CompilerServicesSub 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:
You need to be a premium member to use this feature. To access it, you'll have to upgrade your membership.
Become a sharper developer and jumpstart your career.
$0
$
. 00
monthly
For Basic members:
$20
For Premium members:
$45
For Elite members: