Adding Speech to Your Applications
Introduction
Back in the 80's, speech was a pretty big deal. Everyone was trying to get their Apple, Commodore, or other 8-bit computers to speak to them. Some of it was to report information, but given the constraints of the time, it was generally either for games or pure fun. There's nothing like having your computer call your brother a name! Some games would speak as part of a typing lesson, to act as in-game narration, or to present other information. Sound sampling was starting to be used, but memory was too small to store snippets of every possible spoken phrase. Synthesized speech was the way to go!
These days, games have much less need for dynamic speech. In fact, some games can even string together speech snippets into new sentences pretty convincingly. Most of us don't have the resources to do this though. If we want arbitrary spoken messages, we need to go retro! Welcome back synthesized speech, we missed you!
This article will step you through adding speech to your .NET application. So what can you use the speech for? Any kind of monitoring application can speak the status of something. You could read the subject of incoming emails, the current capacity of a hard drive, the weather, or any other real-time information. This could be very useful for adding accessibility to an application or providing information in a non-obtrusive manner. You'll be surprised how easy it is to use. What you do with it is up to you!
Adding speech Support
As it turns out, Windows has provided built-in speech support for quite awhile. The Microsoft Speech API provides powerful spoken word synthesis with little effort for the developer. The first step is to link to the appropriate library. Unfortunately, speech is not directly accessible as managed code; however the interop work required is negligible. After a few clicks, you'll be speaking like the best!
Start by creating a new solution in C# or VB. I haven't tried this, but I see no reason why J# wouldn't work either if you are working in that realm. Linking to the COM object is as simple as adding a reference to the library.
With your new solution and project open, in the Solution Explorer, right-click on References, then click Add Reference. From the Add Reference dialog, click the COM tab, then select Microsoft Speech Object Library and click OK:
Theoretically, you could start to speak now with just a line or two of extra code, but for convenience (and to stretch this article...) let's encapsulate the speech behavior into a helper class.
In Solution Explorer, right-click the project, then click Add | Class. In the Add New Item dialog, for Name, enter €œSpeechUtility. €
Figure 2: Creating the SpeechUtility class
The primary feature you are probably waiting for is the ability to speak a sentence. This is as short as one line of code. Before adding any code, it will be more convenience to add the SpeechLib namespace with the using statement. At the top of the file, after €œusing System.Text; € add:
using SpeechLib;
This will save time when using classes from the namespace. In order to speak a command though, you'll need to instantiate the SpVoice class. Instead of creating a fresh one every time, and taking the associated memory and performance hit, create a static object to reuse each time. At the class level, after the €œclass SpeechUtility € declaration, add the following:
private
static
SpVoice
objSpeech =
new
SpVoice
();
The €œstatic € keyword is used to indicate that even if you create 100 objects from this class, this object is shared. The static keyword can also be used at the method level (as you will see). In a similar fashion, this means that the method is not associated with a given class. It can access any static member variables, but if you also have class-level instance variables, you can't access them. For this class, we will only use static data and methods so no troubles. Making it instance-based works fine as well, but then you need to be able to pass around the instance throughout the code. Since we gain no benefit from a distinct instance, it is more convenient this way.
Let's Talk!
You are almost ready to start talking! The Speak method of the objSpeech object does the work of invoking the speech synthesis. The first argument is a string, in this case the message to say (though it can vary). The second argument instructs the speech engine what to do with the first argument. This can be used to speak text from a file, speak directly to a WAV file, or to pass other instructions to the engine. Create our Speak method as the following: