Entries:
Comments:
Posts:

Loading User Information from Channel 9

Something went wrong getting user information from Channel 9

Latest Achievement:

Loading User Information from MSDN

Something went wrong getting user information from MSDN

Visual Studio Achievements

Latest Achievement:

Loading Visual Studio Achievements

Something went wrong getting the Visual Studio Achievements

Giving Computers a Voice

  This article demonstrates how easy it is to enable managed applications to finally talk back using Microsoft's Speech API (SAPI).

Difficulty: Easy
Time Required: 1-3 hours
Cost: Free
Software: Visual Studio Express Editions
Hardware:
Download:

 

Summary

Most of us talk to our computers all the time. You wouldn't believe the things my wife says to her poor machine when something goes wrong. This article demonstrates how easy it is to enable managed applications to finally talk back using Microsoft's Speech API (SAPI). But don't worry: since you control what the application says, you can ensure it uses nicer language than my wife.

SAPI

SAPI is the speech API that gives applications access to speech recognition and text-to-speech (TTS) engines. This article focuses on TTS. For TTS, SAPI takes text as input and uses the TTS engine to output that text as spoken audio. This is the same technology used by the Windows accessibility tool, Narrator. Every version of Windows since XP has shipped with SAPI and an English TTS engine.

TTS puts user's ears to work. It allows applications to send information to the user without requiring the user's eyes or hands. This is a very powerful output option that isn't often utilized on PCs.

Three steps are needed to use TTS in a managed application:

  1. Create an interop DLL

    Since SAPI is a COM component, an interop DLL is needed to use it from a managed app. To create this, open the project in Visual Studio. Select the Project menu and click Add Reference. Select the COM tab, select "Microsoft Speech Object Library" in the list, and click OK. These steps add this reference to your project and create an Interop.SpeechLib.dll in the same folder as your executable. This interop DLL must always be in the same folder as your .exe to work correctly.

  2. Reference the interop namespace

    Include this namespace in your application. In C#, add "using SpeechLib;"; iIn VB, add “Imports SpeechLib”.

  3. call Speak()

    Create a SpVoice object and call Speak():

    Visual C#

    SpVoice voice = new SpVoice();
    voice.Speak("Hello World!", SpeechVoiceSpeakFlags.SVSFDefault);

    Visual Basic

    voice = New SpVoice
    voice.Speak("Hello World!", SpeechVoiceSpeakFlags.SVSFDefault)

That's it! The downloads for this article are simple C# and VB.NET "hello world" samples to try out.

Another Example

One of the best uses of TTS is for audio notifications. The System Monitor sample on coding4fun is a perfect candidate to be TTS-enabled. It informs a user when a particular system event has occurred through a message box, system tray balloon tip, or email. I'll use its extensible notification infrastructure to add TTS to this list.

First, create an interop DLL and add a reference to the namespace using the steps above.

Second, add this file to the SystemMonitor project:

SpeechNotifier.cs:

Visual C#

using System;
using SpeechLib;
namespace SystemMonitor.Notifiers
{
class SpeechNotifier : NotifierBase
{
public SpeechNotifier()
{
_voice = new SpVoice();
}

public override void Execute(string title, string message)
{
string text = "System monitor warning! " + title;
_voice.Speak(text, SpeechVoiceSpeakFlags.SVSFDefault);
}

private SpVoice _voice;
}
}

SpeechNotifier.vb:

Visual Basic

imports System
imports SpeechLib

Namespace SystemMonitor.Notifiers

Class SpeechNotifier Inherits NotifierBase

Public Sub New()
_voice = new SpVoice
End Sub Public Overrides Sub Execute(title As String, message As String)

String text = "System monitor warning! " + title;
_voice.Speak(text, SpeechVoiceSpeakFlags.SVSFDefault);
End Sub Private SpVoice _voice
End Sub End Class End Namespace

Third, add this line in the App.config file to any monitors you want to be spoken:

<notifier type="SystemMonitor.Notifiers.SpeechNotifier,SystemMonitor" />

 

For example:

<monitor runFrequency="00:04" type="SystemMonitor.Monitors.DiskSpaceMonitor,SystemMonitor">
<settings>
<setting name="driveLetter" value="C" />
<setting name="freeMegabytes" value="10000" />
</settings>
<notifiers>
<notifier type="SystemMonitor.Notifiers.SpeechNotifier,SystemMonitor" />
</notifiers>
</monitor>

 

This example will say "System monitor warning! Disk space low." when the space on c:\ drops below the specified value.

General TTS Tips

  • Keep it short. The voice used by the default XP TTS engine is not exactly soothing. Most users would not want to hear it speak long texts, but it often works well for conveying short, functional pieces of information.
  • Have a preface. Many TTS apps are designed to be run in the background and speak audio when the user is focused on a different application. In this case it's helpful to let the user know the context. It wouldn't make sense if your computer randomly said "Bill Gates," but you would know what "New mail from Bill Gates" meant.
  • Keep it "speakable." Test out the text your app intends to say. The default TTS engine has some limitations you can avoid. For example, "c:\" would be pronounced "see colon backslash". This can be avoided by speaking just "c" instead.
  • Change defaults. The speech control panel allows the user to select the default TTS engine (for computers with more than one installed) and set the default speaking rate.

Cool project ideas

The TTS functionality on PCs is an under-used resource with much promise. Here are some more project ideas:

  • GotDotNet has the source code of a few email and RSS readers. Reading the title of a message as it arrives in the background would be a great use of TTS.
  • The System Monitor sample above just scratches the surface of event notifications. That application could be the foundation for monitoring any kind of system resource or application event. And with TTS you can let your ears do the work while your hands and eyes are busy.
  • The It's Hot in Here coding4fun sample uses a web service to read weather forecasts. Don't bother checking a web page for the weather; just modify this app to read it with a hotkey while your eyes do more important things. TTS is great for any similar web service update application: news headlines, stock quotes, sports scores, auction prices, and more.
  • Perhaps more importantly, that same article also introduces us to Phidgets. Upstage the guy next door by sending your Phidget battlebot into the fray with a TTS "You killed my father, prepare to die!".

What's next?

This article outlines how you can use TTS on PCs today. But the real story is what's coming up.

WinFX will contain the fully managed API for speech. No more ugly COM interop. Developers will get all the benefits of a managed API and full intellisense support.

The default TTS engine on Windows Vista (formerly Windows code name "Longhorn") is much better than the default XP engine. Check out some of the resources below for updates as Windows Vista gets closer to shipping.

Resources

Here's some blogs from members of the Speech Components Group at Microsoft:

Tags:

Follow the Discussion

  • Prashant DPrashant D

    Thank you this document helped me a lot..

  • Max R.Max R.

    Hello! Very interesting. Thank you.

  • thecobrathecobra

    cool totoring but were do u use speach to text

  • ArmandoArmando

    Hi!

    There`s a way to do that but using a Spanish TTS?

    Thanks!

  • NevinNevin

    This is a good tutorial, but is there any way to use the same funtionality on a web application.  I have built one that works fine on a local server, but when deployed to the remote server, it has problems accessing the Speechlib in the BIN folder without creating an error.

    Nevin

  • ahmed awadahmed awad

    thank you very much but if you can send me how can i read from more languages specialy arabic and frensh.

    my mail (elbanna23@yahoo.com)

    thank you again

    bye

  • Noticias externasNoticias externas

    It is not easy to find good and free Text to Speech libraries for your .NET application. Even the solution

  • one software developerone software developer

    I have worked on that but it does not have so good quality. For excellent quality there is aproject from AT&T. its free and you can use <a href="http://developeronline.blogspot.com/2008/01/text-to-speech-for-masses.html"> text-to-speech C#</a>

  • Clint RutkasClint I'm a "developer"

    @Deepak Trama:  If you know the text you're going to say, create a dictionary before hand and their lengths.

    Without diving into the SAPI api, I couldn't tell you if it does or doesn't have that functionality.  Sorry.

  • Deepak TramaDeepak Trama

    Hi,

    I'm writing an app which generates a .wav file from some text.

    I need to sync some pictures with this text and present it to the user as a slide show.

    'Sync' is a relative term here, I need the pictures to appear some what close to the words when they are spoken.

    Is there some way (a SAPI api) which will tell me where (duration in seconds) a particular word would occur in the generated audio file.

    Any ideas?

    My email is zodiac.seven@gmail.com

  • RitchieRitchie

    This is a good very good article. There seems to be quite a bit of information on TTS. But, can somebody help me with STT Speech-To-Text conversions? RichardMaliwatu@hotmail.com

  • mehwishmehwish

    i want to know that is it possible to convert voice to digital signal and then to identify the speaker...

    if yes then plz provide me the C# code for voice recognition...

    thank you!!

  • adayaday

    Thanks very much for this excellent tutorial.

    Can you teach me how to choose other voices in c# ?

  • venkatvenkat

    does SAPI supports multi languages like german,spanish.

  • aminamin

    hi

    it was so interesting article.

    pleas introduce me some more example about speech recognition.I need something simple with no grammer recognition. something that just understand one word.

    thanks alot

    amin_mhsn@yahoo.com

    amin

  • HeathHeath

    Check out http://www.iSpeech.org/  The work is already done for you.  Not to mention it uses good voices and it's free.

  • vigneshkumarvigneshkumar

    Thanks a lot for the information provided above

  • MattMatt

    Easier than that complicated code above:

    You can just have a textbox and a button, and on the button type:

    Dim SAPI

           SAPI = CreateObject("sapi.spvoice")

           SAPI.Speak(TextBox1.Text)

    and you can replace the Textbox1.Text to "Anything In parenthesis" to make SAPI say something specific when that button is clicked

  • Clint RutkasClint I'm a "developer"

    @Mark, attempting to track this down as I'm not a SAPI expert.

  • MarkMark

    Hi,

    Is is possible to set an audio stream as an input to the SpeechRecognitionEngine, SpInProcRecognizer ?

    This stream will NOT be a stream of fixed size. I will keep writing byte arrays to this stream one after the other.

    Thanks in advance,

    Mark

  • Clint RutkasClint I'm a "developer"

    @Mark Yes. This is possible by implementing your own com object that implements the ISpStreamFormat interface. I don't know of any samples that demonstrate this precisely, though.

  • chintanchintan

    Hi,

    Great tutorial. I wanted to know that can i use the british voice instead of american voice ? if yes then please let me know how ? I am using windows 7 and developing a web application on asp.net 3.5 in visual studio 2010.

    Thanks in advance,

    Chintan

  • azqar ahmadazqar ahmad

    good man ,good job,keep it up and give programmers those things which they dont know about

  • Clint RutkasClint I'm a "developer"

    @יעל  if I translated this correctly, "How can I change the tone of speech", Not sure you can change the voice programmatically.

  • soccer football boots on salesoccer football boots on sale

    [/urlWe sell the well-known[url= http://soccerfootballboots.com]soccer football boots on sale[/url].You can choose which you like,[url= http://soccerfootballboots.com]Nike football boots],[url= http://soccerfootballboots.com m]Adidas football boots[/url]are both hot sale, We sell[url=http://soccerfootballboots.com]football boots online[/url]large quantities. Welcome to our site http://soccerfootballboots.com

  • RubelRubel

    Thanks a lot, hope this will help me a lot

  • EricEric

    I get an error at the line where you create the new object: Abort During Order Evaluation: Incorrect Order Number

  • RaymondRaymond

    Awesome, much easier than the system.speech namespace! :)
    Works well in websites

  • ArchiekinsArchiekins

    please how do i use speech to activate a command button click in vb 6?!i have downloaded SAPI 5.1. need help asap...

Remove this comment

Remove this thread

close

Comments Closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.