• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL

4.3. Speech Interface

Talking computers are the coolest thing. When I was a kid I had this software for my Commodore 64 that would talk, and I would spend hours making it say stupid, stupid things. (Hey, if you grew up in Pflugerville, Texas, you'd probably have been driven to such nonsense too.)

First, the good news: computers can still talk! Now, the bad news: they still don't talk very well, and putting voice synthesis into a script isn't as easy as I would have hoped.

That said, AT&T Natural Voices is fantastic. It can be trained to the point that you can hardly tell that it's not a human being. It's not free, but if really good synthesized speech is important to you, it might be worth the cost.

A great deal of development time has been devoted to improving computer voice synthesis, largely because it provides an interface for physically challenged people who can't use a monitor. There is also a crowd of people who believe voice will be the way people interact with computers in the future. I'm not sure that voice is ever going to be that popular, though I do appreciate it that my PDA speaks driving directions to me. Otherwise, I'd rather just read something from the screen than have the computer read it aloud.

This project requires a command-line tool that can be called from a script to speak a phrase. I couldn't find a free tool on the Web that worked well, so after a great deal of research, I decided I was going to have to write my own program. Microsoft provides a speech API with Windows, so I decided to take advantage of that by using C# and Visual Studio .NET.

This little speech app has a lot of uses. If you're an IRC freak like myself, you can add voice to your favorite channel. I use Mirc as my chat client, and the following remote script speaks every line that someone types (as long as it's not more than 29 words):

on 1:TEXT:*:#banter:/run -np C:\homehacking\ReadWeather\Say $nick says $1 $2 $3 $4 $5 $6 $7 $8 $9 $10 $11 $12 $13 $14 $15 $16 $17 $18 $19 $20 $21 $22 $23 $24 $25 $26 $27 $28 $29
on 1:ACTION:*:#banter:/run -np C:\homehacking\ReadWeather\Say $nick $1 $2 $3 $4 $5 $6 $7 $8 $9 $10 $11 $12 $13 $14 $15 $16 $17 $18 $19 $20 $21 $22 $23 $24 $25 $26 $27 $28 $29

If you are using Linux for this project, I would use the Festival speech synthesis system and the Perl voice synthesis modules. You can download Festival from http://www.cstr.ed.ac.uk/projects/festival/. The Perl modules that work with Festival can be found at CPAN, at http://search.cpan.org/~rcaley/speech_pm_1.0/. You can theoretically use Festival on Windows, too, though I couldn't find any documentation that had been updated since Windows was still called "NT."

4.3.1. 1. Find SAPI.DLL

You'll need the Microsoft Dynamic Link Library (DLL) that contains the Speech Application Programming Interface (API). It's called SAPI.DLL, and if you have a recent version of Windows or Office, you probably already have it. You can do a search for the file sapi.dll, or you can open a command prompt and execute the command dir /s \sapi.dll.

If you find this file, make note of its location—you'll need to identify it when you write your C# application. If you have multiple versions, make note of where the newest version lives. If you can't find the file or just want to make sure you have the latest version, download the Speech SDK or the Speech Application SDK from http://www.microsoft.com/speech/. (Be warned—the SDKs are a huge download.) The only file you need to extract is the sapi.dll file; once you've dug it out, store it in your %systemroot%\system32\ directory.

4.3.2. 2. Create the speech console application

This step assumes that you have Microsoft Visual Studio .NET and the .NET Framework installed. If you don't want to use Visual Studio, you can compile this application using free tools that Microsoft provides, or, even easier, you can visit http://www.homehacking.com/ and download the executables.

  1. Launch Microsoft Visual Studio .NET.

  2. Go to File New Project.

  3. In the New Project dialog, click Visual C# Projects, then click Console Application.

  4. In the Name field, type Say, as shown in Figure 4-5.

    Figure 4-5. Creating a console application that talks.

  5. Click OK.

  6. Click the Project menu, then click Add Reference.

  7. Click the COM tab, then double-click the highest version of the Microsoft Speech Object Library available. If it's not in the list, click the Browse button and locate your sapi.dll file.

  8. Click OK.

  9. Replace the default Class1.cs code with the following:

    	using System;
    	namespace Say
    	  using System.Threading;
    	  using SpeechLib;
    	  class Class1
    		static void Main(string[] sentence)
    		   string toSpeak = "";
    		   foreach(string word in sentence)
    		      toSpeak = toSpeak + word + " ";
    		   toSpeak = toSpeak.Trim();
    		   if ( toSpeak.Length > 0 )
    		      Console.WriteLine("Saying: \"" + toSpeak + "\"");
    			  SpeechVoiceSpeakFlags SpFlags = SpeechVoiceSpeakFlags.SVSFlagsAsync;
    			  SpVoice Voice = new SpVoice();
    			  Voice.Speak(toSpeak, SpFlags);
    			  Console.WriteLine("What you talkin 'bout, Willis?");

  10. On the Standard toolbar, click the list that currently shows Debug, then click Release. (This ensures that you are using the release build configuration.)

  11. Click the Build menu, then click Build Solution.

And that's it—the Say console application is ready to go. Let's test it out. Open a terminal and switch to the directory your project was configured to build in (by default, this will be C:\Documents and Settings\username\My Documents\Visual Studio Projects\Say\bin\Release). Then execute the following command:

	Say Eric John Parucki is my God.

Your computer will swear allegiance to Eric, and it's wise that you do it, too.

Notice that there are two files in the Release directory: Say.exe and Interop.SpeechLib.dll. Visual Studio wrapped up the sapi.dll library so that your .NET Framework application could access it. Copy both files to the C:\homehacking\ReadWeather\ folder (remember to move both files if you end up moving the application to a different directory). If you move the application to a different computer, make sure the computer has the .NET Framework installed; you can download the .NET Framework from http://windowsupdate.microsoft.com.

Everyone knows you're making your computer say dirty things, so just stop it.

  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint