Blind Geeks dot org

VBLessons.com

Simple Text-To-Speech (TTS) Example

Instructions

 

1.                Install Microsoft Speech Engine 5.1.  This can be found at MS Speech API

2.                Open Microsoft Visual Studio 2005 (This should work in Visual Studio 2008 as well) in either Visual Basic or C# and create a new windows forms application.

3.                Add a Reference to the Microsoft Speech Object Library in the Projects Menu and in the COM tab.

4.                Place the desired controls onto your form.  It is recommended to place two buttons (btnSpeak, btnExit) and a text box (txtSpeak).

5.                Create the variable that you will be referencing in your speech commands.  Make this of type SpeechLib.spVoice.  See C# and VB examples below.

 

C# Example:    public SpeechLib.SpVoice SPVoice;

 

VB Example:

 

Imports SpeechLib

Public Class Form1

Public sp_Voice As SpVoice

 

 

6.                Create a Form Load event handler that instantiates the sppech variable declared in the step above and speaks to the user to instruct them to enter text into the text box and activate the Speak button.  See code samples below.

 

C# Example:

 

        private void Form1_Load(object sender, EventArgs e)

        {

            //Set up our variable and introduce the program.

            SPVoice = new SpeechLib.SpVoice();

    SPVoice.Speak("Type what you would like me to speak into

the text box then click the Speak button.",         SpeechLib.SpeechVoiceSpeakFlags.SVSFlagsAsync);

        }

 

 

VB Example:

 

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As

              System.EventArgs) Handles MyBase.Load

        sp_Voice = New SpVoice

        sp_Voice.Speak("Please type what you wish me to speak in the text

box then activate the Speak button.", SpeechVoiceSpeakFlags.SVSFlagsAsync)

    End Sub

 

 

7.                Create a btnExit event handler that speaks to the user, announcing that the application is about to end.  Place a loop after the speak command so that the speech engine has enough time to finish speaking before exiting the program.  Then, have the program end.  See code examples below.

 

C# Example:

 

        private void btnExit_Click(object sender, EventArgs e)

        {

            // Saluations - Bye!

            SPVoice.Speak("Have a Nice Day!  Goodbye.",

 SpeechLib.SpeechVoiceSpeakFlags.SVSFlagsAsync);

            int i=0;

            // Time delay to make sure saluations get spoken before

//ending program.

            while (i<999999)

            {

                int j = 0;

                while (j < 1999)

                {

                    j++;

                }

                i++;

            }

            // end the program now.

            Application.Exit();

        }

 

VB Example:

 

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As

              System.EventArgs) Handles btnExit.Click

        sp_Voice.Speak("Have a nice day!   Goodbye.",

              SpeechVoiceSpeakFlags.SVSFlagsAsync)

        Dim i As Integer

        For i = 0 To 99999

            Dim j As Integer

            For j = 0 To 199999

                j += 1

            Next j

            i += 1

        Next i

        Application.Exit()

    End Sub

 

 

8.                Create a btnSpeak event handler.  Place code in this handler that speaks the text typed into the text box.

 

C# Example:

 

        private void btnSpeak_Click(object sender, EventArgs e)

        {

            // The speak button has been activated.  Speak the text in the

              //Text Box.

            SPVoice.Speak(txtSpeech.Text,

              SpeechLib.SpeechVoiceSpeakFlags.SVSFlagsAsync);

        }

 

VB Example:

 

    Private Sub btnSpeak_Click(ByVal sender As System.Object, ByVal e As

              System.EventArgs) Handles btnSpeak.Click

        sp_Voice.Speak(txtSpeech.Text,

              SpeechVoiceSpeakFlags.SVSFlagsAsync)

    End Sub

 

 

9.                Save and run your program in debug mode.  Test your work.

 

This Lesson can be found at ftp://blindgeeks.org/TTSSimple.html

 

The Example Projects can be found at:

 

C Sharp:    ftp://blindgeeks.org/SimpleTTS_CS.zip

Visual Basic:       ftp://blindgeeks.org/SimpleTTS_VB.zip