Advertisment

Build a Talking Notepad

author-image
PCQ Bureau
New Update

Tired of reading text on your computer screen? Want someone to read it to you? Don’t worry, help is at hand. You can code your own speaking Notepad that can read out whatever text you give it. To do all this, you require Visual Basic 6.0. You’ll find other required files (spchapi4.exe, l&htts.exe and msttsa22L.exe) and the source on this month’s DVD (Developer’s Lab section). 

Advertisment

If you want the ready-made compiled application, then run setup.exe file in the binaries directory and the application will be installed. Before compiling or installing the application, run the files, spchapi4.exe, l&htts.exe and msttsa22L. exe. To take a look at the code and compile the application for yourself, open the Project1.vbp file, found in the source directory, with Visual Basic 6.0.

Three layers that make the app
User 



Application
Speech API
TTS Engine

The complete application is divided into three parts. The first is a TTS (Text to Speech) engine, which converts text into speech. The files msttsa22L.exe and l&htts.exe install two such engines. A single engine can contain various voice styles. You can also use any other engine with this application and it will be automatically recognized. Your user application uses these TTS engines to convert text into speech. To use the engine, your application needs some APIs (Application Programming Interface). This is where the Speech API comes in. It takes in commands from the application and passes them on to the TTS engine. And, takes in parameters and status from the engine and passes them on to your application. This way it creates an interface between your application and the engine. That is why it is called Application Programming Interface. The file spchapi4.exe installs the Speech API on your system. The Speech API contains many different files, of which we use VText.dll that is programmed using the Microsoft Voice Text control in the Visual Basic development environment.

Advertisment

To make the application, start with a standard exe project with a blank form. On the form you will put the various controls, command buttons and menus. Place a text box on the form, which will hold the text to be spoken. Put command buttons for play, pause, stop, rewind and forward. Put in a menu bar. Now put a ComboBox, which will list the various voice styles available. From the project components window, select Microsoft Voice Text Control and Microsoft Windows Common Controls 6.0. Now put the TextToSpeech control and slider on your form. The slider is to control the speed of spoken text and the TextToSpeech control is what will perform all the functionality. Name the TextToSpeech control TextToSpeech1.

With all the visual elements in place, it is time for coding. We will start with the

Form_Load() subroutine.

Private Sub Form_Load()



Dim strVoiceType As String


Dim intEngine As Integer


Dim i As Integer


intEngine = TextToSpeech1.Find(“”)


TextToSpeech1.Select intEngine


For i = 1 To TextToSpeech1.CountEngines


strVoiceType = TextToSpeech1.ModeName(i)


Combo1.AddItem strVoiceType


Next i


Combo1.ListIndex = TextToSpeech1.CurrentMode - 1


TextToSpeech1.Speed = 70 + 10 * Slider1.Value


End Sub










Advertisment

This code executes when the first form loads and initializes some of the parameters of the application. First, it finds the index of the current voice style. Then it selects that particular voice style to use for the application. After that the combo box is filled with all available voice styles, to help you choose from them afterwards. The current voice mode is then displayed in the combo box. The slider value then sets the default speed of reading text. There is a default value set for the slider control.

Then you have the code for the speak button. The speak button calls the subroutine speak1 which has the following code.

Private Sub speak1()



If Text1.SelLength = 0 Then


If (Text1.SelStart = 0) Or (Text1.SelStart = Len(Text1.Text)) Then


TextToSpeech1.Speak Text1.Text


Else


userselect = False


Text1.SelLength = (Len(Text1.Text) - Text1.SelStart)


TextToSpeech1.Speak Text1.SelText


End If


Else


userselect = True


TextToSpeech1.Speak Text1.SelText


End If


End Sub











Advertisment

It checks whether the user has selected some text on the text box or wants the entire text to be spoken. Then it passes the desired text to the ‘speak’ method of the TextToSpeech control and the text is spoken out.

The application can also read text copied to your clipboard by copying the clipboard text into a string variable and then passing that variable to the ‘speak’ method.

Private Sub mnuRead Clipboard_Click()



Dim clipstr As String


clipstr = Clipboard.GetText


If clipstr <> “” Then


TextToSpeech1.Speak clipstr


End If


End Sub




Advertisment

When you quit the application, the code in the Form_Unload() function first ensures that the application is not speaking some text, if it is then it first stops it and then quits the application.

The rest of the code is for performing other functions, such as basic text editing, changing voice styles through the combo box, changing read speeds through the slider, to stop, pause, rewind and forward the speech and is self explanatory. 

The advanced settings let you create customized pronunciation of words and allows you add them to the dictionary. You can even select the phonemes, which make up the pronunciation of a particular word.

This was a basic application displaying the might of converting text to speech and the tools available for programmers. Other tools, with a lot of properties and methods, are also available. You can add more features to this application, making it a full-blown application capable of doing all this and a lot more.

Anoop Mangla

Advertisment