Speech and REALbasic
Volume Number: 15 (1999)
Issue Number: 12
Column Tag: Programming Techniques
Speech and REALbasic
by Erick Tejkowski
Add powerful speech and recognition abilities to your REALbasic application
Introduction
Ever since the first Macintosh arrived, speech has been an interesting addition to the Mac OS. Computerized speech has been a mainstay on the Mac since day one. It has evolved over the years into its current state, changing names a couple times along the way. The ability of having your computer speak to you is invaluable, leading to the development of a variety of applications. You can now have your email, caller ID, or web pages read to you thanks to Apple's speech capabilities. A little later, the Mac OS began shipping with English speech recognition software. Instantly you could control parts of the Macintosh through AppleScripts in the Speakable Items folder. There is also a SDK available from Apple that shows how to implement speech recognition in your own applications. Although it has been possible for a number of years now, it has not always been particularly easy to implement. The SDK required extensive knowledge of C++ object programming. Luckily, some thoughful programmers have put together a whole collection of tools to control and use speech and speech recognition in your own applications.
What this means for REALbasic programmers is that you now have convenient access to all of the aforementioned speech abilities. With little effort, you can quickly have your computer barking out phrases. Furthermore, you can implement speech recognition in your own applications. REALbasic offers several manners in which to implement each of these functions. This article will attempt to cover all of the various ways to add speech and speech recognition abilities to REALbasic applications. Advantages and disadvantages will also be discussed for each of the methods.
Text to Speech
Getting your REALbasic application to speak can be accomplished in a number of ways. Deciding on which method is most appropriate or convenient for your purposes can most easily be accomplished by examination of each method. As we discuss each of the methods, we will build a sample application in REALbasic.
AppleScript
AppleScript is a fast and simple way to add speech abilities to your REALbasic application. REALbasic is capable of directly calling AppleScripts and AppleScript is able to control the Text-to-Speech abilities of the Mac OS. To demonstrate, begin by opening REALbasic and starting a new project. To Window1 add an EditField and a PushButton. Change the Caption Property of the PushButton to "AppleScript" and put the following code in the Action event:
Sub Action()
dim i as string
i=SaySomeString(Editfield1.text)
End Sub
The command SaySomeString is the name of the AppleScript we will use to speak a string of text; the text contained in the EditField, to be exact. Next, start the AppleScript Script Editor application and write a simple script as follows:
on run {x}
tell application "Finder"
say x
end tell
end run
The variable x is the string we will pass to the script from REALbasic. The On Run statement allows AppleScript to accept a value from outside AppleScript. Save the script as a "Compiled Script" and name it "SaySomeString". Next, drag the compiled script into your REALbasic project. By now, your REALbasic project should look like Figure 1.
Figure 1. Speech via AppleScript.
Select Run from the Debug menu to test the project so far. Type some text in the EditField and press the PushButton. Your Mac should speak the text. Now, wasn't that simple? One disadvantage of this method is that the computer has to rely on AppleScript to perform the speech command. Since AppleScript is acting as the intermediate between your application and the operating system, performance can sometimes be a tad slow (particularly on older machines). Moreover, AppleScript must be installed for this to work properly.
To avoid the dependence on AppleScript, several other methods can be used to accomplish speech. They include shared libraries, native system calls, and REALbasic plugins. To demonstrate each of these methods, we will add three more PushButtons to the Window1 of our project. Set the Caption property of each PushButton to read "SharedLib", "SystemCall", and "Plugin" respectively.
Shared Libraries
Shared Libraries are collections of pieces of code that exist in your System Folder (typically, but not always, in the Extensions Folder) and are simultaneously available to multiple applications. The speech functions are accessible through commands to the SpeechLib Shared Library. To add the SpeechLib to your project, drag the Speech Manager Extension from the Extensions Folder into the REALbasic project window. Once the SpeechLib has been added to the project, REALbasic must be told what functions to utilize from the library. This is accomplished through "Entry Points". To add an entry point, double-click the SpeechLib in the project window. A window will appear that allows you to add entry points. Click the Add button and enter the following information:
Listing 1. NewSpeechChannel Entry Point for SpeechLib
Name: NewSpeechChannel
Parameters: Ptr, Ptr
Return Type: Integer
Be certain to enter this information exactly as printed, since case matters here. This information tells REALbasic to add a command NewSpeechChannel which accepts two pointers as parameters and returns an integer.
Similarly, add entry points with the information in Listing 2.
Listing 2. The remaining Entry Points for SpeechLib
Name: DisposeSpeechChannel
Parameters: Ptr
Return Type: Integer
Name: SpeechBusy
Parameters:
Return Type: Integer
Name: SpeakText
Parameters: Ptr, Ptr, Integer
Return Type: Integer
Name: GetIndVoice
Parameters: Integer, Ptr
Return Type: Ptr
Next, create the following properties in Window1 by selecting the Edit...New Property menu.
ChannelPtr(0) as MemoryBlock
text(0) as MemoryBlock
Voice(0) as MemoryBlock
Finally, add code to the Window1 Open event and to the PushButton2 Action event as shown in Listing 3.
Listing 3.
Window1.Open:
Sub Open()
ChannelPtr(0) = newmemoryBlock(4)
text(0) = newmemoryBlock(255)
End Sub
Window1.PushButton2.Action:
text(0).Cstring(0) = Editfield1.text
OSerr SpeechLib.GetIndVoice(7,Voice(0))
OSerr SpeechLib.NewSpeechChannel(Voice(0), ChannelPtr(0))
OSErr SpeechLib.SpeakText(ChannelPtr(0).Ptr(0),text(0),
len(text(0).Cstring(0))) // This is all one line!
while (SpeechLib.SpeechBusy() <> 0)
wend
OSerr SpeechLib.DisposeSpeechChannel(ChannelPtr(0).Ptr(0))
End Sub
Select the Debug...Run menu to test it. Type in some text into EditField1 and press the "SharedLib" button. You should hear your text spoken back to you in Kathy's voice. This occurs with no reliance on AppleScript.
To learn more about Shared Libraries, please check the references at the end of this article. In particular, look at Christian Brunel's site. He has a detailed explanation of working with Shared Libraries in REALbasic. In fact, the code presented here is a scaled-down version of his. He goes into much more detail, so don't miss it!
System Toolbox Calls
If all of the shared library preparation seemed a bit daunting, never fear. REALbasic has added the ability to make system level Toolbox calls. To call system APIs from REALbasic, you must use the Declare statement. While not necessarily a topic for pure beginners, a freeware application entitled TBFinder (listed in the References at the end of this article) by Fabian Lidman helps tremendously. Furthermore, Matt Neuberg's book also discusses the topic in greater depth. For speech purposes, our sample application will make use of one Declare statement. Add a third PushButton to Window1 and change the caption property to read "System Call". Double click PushButton3 and enter the code in Listing 4 into the Action event of the PushButton.
Listing 4
dim s as string
dim i as integer
Declare Function SpeakString lib "SpeechLib"
(SpeakString as pstring) as Integer //This is all one line!
s=editField1.text
i=SpeakString(s)
This example comes to you directly from the REALbasic Developer Guide. It shows how you can eliminate all of the messy steps involved with the previous shared library call in one statement. The Declare function can call any number of system level calls. A good way to discover them is to read Inside Macintosh and the Mac OS Universal Headers included with CodeWarrior. Again, be sure to look at TBFinder. It takes away much of the guesswork of Declare statements. Since Declare statements deal directly with the system level APIs, the parameters and return types are often C data types that might be unfamiliar (if not downright scary!) to the REALbasic programmer. Some of this ugliness was the reason folks flocked to REALbasic in the first place, which leads to the next topic.
REALbasic Plugins
The final manner in which we can get REALbasic to speak text is by using a native plugin. REALbasic supports its own native plugin format. Simply drop a plugin into the Plugins folder located within the same folder as the REALbasic application. Restart the REALbasic application and it will now have the added functionality that the plugin provides. You will need to check the documentation for the plugin to learn about its methods and controls it adds. For our example, we will use the VSE Utilities Plugin. It has recently been made freeware and you can download it from the site listed in the Reference section of this article. Add the plugin to your Plugins folder and restart REALbasic, remembering to first save your project before restarting. Once restarted, load the project again, and drag a fourth PushButton onto Window1. Change the Caption property to "Plugin". Double-click PushButton4 and add the code in Listing 5 to the action event of the PushButton.
Listing 5
dim i as integer
i=Speak(Edifield1.text)
The plugin adds a Speak method to REALbasic. All of the work is done behind the scenes. The advantage here should be obvious - simplified code. The drawback is that you are at the mercy of the plugin programmer to properly write the code, to update it regularly, and to code it efficiently. Still, when a plugin is available for a particular function that you need, your work will be drastically reduced by using it. Furthermore, it conforms to the REALbasic programming methodology, which does not require extensive knowledge of the sometimes-complicated Mac Toolbox. Figure 2 shows the completed demo project.
Figure 2. The completed Speech.pi project.
As you can see, REALbasic offers a number of ways to make your Mac speak text. The idea here is not only to show you that speech is possible in four different manners, but that these four methods can be used in other instances of your applications. It is up to you to seek out the vast number of abilities that these methods afford you. When REALbasic does not support a function natively, the programmer may be able to accomplish the task using AppleScript, shared libraries, direct system calls, or plugins.
Speech Recognition
Speech recognition abilities can be added to your Mac by installing Apple's Speech Recognition package. REALbasic currently offers two different ways to add Apple's speech recognition abilities to your application: Speakable Items and a REALbasic plugin. Both rely on Apple's Speech Recognition technology, but vary in how they respond to spoken commands.
AppleScript
When Apple Speech Recognition is installed, a folder entitled Speakable Items is placed in your Apple Menu Items folder. Within this folder are AppleScripts and aliases to files you wish to open. The idea is that when Speakable Items is turned on through the Speech Control Panel, the Mac will listen for the phrases found in the Speakable Items folder. If we were to place our own AppleScript in this folder that would control a REALbasic application we have written, then we could control the Mac with speech. An excellent tutorial about making an AppleScript-able REALbasic application can be found at the RB Monthly site. Follow the tutorial keeping in mind that you want to send speech commands to your own application. Finally, create AppleScripts that control your application and drop them into the Speakable Items folder. You will also want to check the REALbasic documentation for information about how REALbasic applications respond to AppleEvents.
Apple Event Plugin
The second method to implement speech recognition in your own application is a bit more complex, but luckily Matthijs van Duin has made the job much easier. His sample project details the use of speech recognition and offers classes and modules for use in your own projects. In addition, the result is a self-contained speech recognition example without relying on the higher level AppleScript. Instead, his example relies on an AppleEvent plugin for REALbasic called AE Gizmo by Alex Klepoff . The AE Gizmo plugin allows a REALbasic programmer to use AppleEvent strings in Lasso CaptureAE format within REALbasic. So, be sure to also download the Lasso CaptureAE plugin. To say it another way, you need the AE Gizmo plugin and the project from Matthijs van Duin to do self-contained speech recognition with REALbasic. If you would like to do other types of AppleEvents commands using LassoAE results (as the speech recognition example does), then you also need to download LassoAE. This article will not go into detail about the use of AE Gizmo. That is up to you to explore. A version of AE Gizmo also accompanies Matthijs van Duin's example. It is mandatory that you use one of the newest Alpha release versions of REALbasic for the speech recognition example, as it makes use of some new features in REALbasic. You can download the developer releases of REALbasic from the REALbasic download page.
Conclusion
Adding speech and speech recognition capabilities to your applications used to be the stuff of dreams. REALbasic, along with the help of several third party add-ons, gives you the ability to add speech and speech recognition capabilities to your own software. As Apple continues to improve each of these technologies, you will likely be able to reap the benefits with little or no additional code. You can be certain that speech will be a big topic in the future of computers and with your Mac and a copy of REALbasic you can take advantage of speech and speech recognition today. Now get out there and write some speech software!
References
Erick Tejkowski is a Web Developer for the Zipatoni Company in St. Louis, Missouri. He's been programming Apple computers since the Apple II+ and is still waiting for a new version of Beagle Brothers to be released. You can reach him at ejt@norcom2000.com.