Thursday, January 31, 2008

Make ASP.Net Speak Typed Text

Let's start with Microsoft's Speech API (SAPI)

SAPI has a text-to-speech engine (TTS) that we will make use of. In short, SAPI takes text as input and output an audible speech using TTS engine. By the way, every machine with Windows XP has SAPI and TTS.

SAPI is a COM component that needs to be referenced in the project.

Coding Voice Application

First of all, open Microsoft Visual Web Developer and create a new Website. Use the Visual Basic as language. Add a reference to your project. Website > Add Reference > COM > Microsoft Speech Object Library.
Now throw in a text box and a button on your webpage. It may look as below.


The scenario we want to do is to make the computer speak what has been written in the textbox by clicking on the "Speak" button. Double-click on the button to add some code for the button. To access the DLL function, we have to import the namespace SpeechLib as below:

Imports SpeechLib

The next step is to create a SpVoice object.

Dim voice As New SpVoice()

And finally, you can make the computer speak for you!

voice.Speak(txtSpeech.Text)

Below is the complete little code snippet.


Imports SpeechLib

Partial Class _Default

Inherits System.Web.UI.Page

Protected Sub btnSpeak_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSpeak.Click

Try
Dim voice As New SpVoice() ' Creates the voice object

voice.Speak(txtSpeech.Text) ' Speak whatever is written in the textbox

Catch ex As Exception

Throw ex

End Try

End Sub

End Class

Now, you can play around with the voice object by exploring the different methods and properties that the voice object can expose.

To speed up the rate at which the speaker talks, we can do:
voice.Rate = 10


























































No comments: