TweetFollow Us on Twitter

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.

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Fallout Shelter pulls in ten times its u...
When the Fallout TV series was announced I, like I assume many others, assumed it was going to be an utter pile of garbage. Well, as we now know that couldn't be further from the truth. It was a smash hit, and this success has of course given the... | Read more »
Recruit two powerful-sounding students t...
I am a fan of anime, and I hear about a lot that comes through, but one that escaped my attention until now is A Certain Scientific Railgun T, and that name is very enticing. If it's new to you too, then players of Blue Archive can get a hands-on... | Read more »
Top Hat Studios unveils a new gameplay t...
There are a lot of big games coming that you might be excited about, but one of those I am most interested in is Athenian Rhapsody because it looks delightfully silly. The developers behind this project, the rather fancy-sounding Top Hat Studios,... | Read more »
Bound through time on the hunt for sneak...
Have you ever sat down and wondered what would happen if Dr Who and Sherlock Holmes went on an adventure? Well, besides probably being the best mash-up of English fiction, you'd get the Hidden Through Time series, and now Rogueside has announced... | Read more »
The secrets of Penacony might soon come...
Version 2.2 of Honkai: Star Rail is on the horizon and brings the culmination of the Penacony adventure after quite the escalation in the latest story quests. To help you through this new expansion is the introduction of two powerful new... | Read more »
The Legend of Heroes: Trails of Cold Ste...
I adore game series that have connecting lore and stories, which of course means the Legend of Heroes is very dear to me, Trails lore has been building for two decades. Excitedly, the next stage is upon us as Userjoy has announced the upcoming... | Read more »
Go from lowly lizard to wicked Wyvern in...
Do you like questing, and do you like dragons? If not then boy is this not the announcement for you, as Loongcheer Game has unveiled Quest Dragon: Idle Mobile Game. Yes, it is amazing Square Enix hasn’t sued them for copyright infringement, but... | Read more »
Aether Gazer unveils Chapter 16 of its m...
After a bit of maintenance, Aether Gazer has released Chapter 16 of its main storyline, titled Night Parade of the Beasts. This big update brings a new character, a special outfit, some special limited-time events, and, of course, an engaging... | Read more »
Challenge those pesky wyverns to a dance...
After recently having you do battle against your foes by wildly flailing Hello Kitty and friends at them, GungHo Online has whipped out another surprising collaboration for Puzzle & Dragons. It is now time to beat your opponents by cha-cha... | Read more »
Pack a magnifying glass and practice you...
Somehow it has already been a year since Torchlight: Infinite launched, and XD Games is celebrating by blending in what sounds like a truly fantastic new update. Fans of Cthulhu rejoice, as Whispering Mist brings some horror elements, and tests... | Read more »

Price Scanner via MacPrices.net

Apple’s 24-inch M3 iMacs are on sale for $150...
Amazon is offering a $150 discount on Apple’s new M3-powered 24″ iMacs. Prices start at $1149 for models with 8GB of RAM and 256GB of storage: – 24″ M3 iMac/8-core GPU/8GB/256GB: $1149.99, $150 off... Read more
Verizon has Apple AirPods on sale this weeken...
Verizon has Apple AirPods on sale for up to 31% off MSRP on their online store this weekend. Their prices are the lowest price available for AirPods from any Apple retailer. Verizon service is not... Read more
Apple has 15-inch M2 MacBook Airs available s...
Apple has clearance, Certified Refurbished, 15″ M2 MacBook Airs available starting at $1019 and ranging up to $300 off original MSRP. These are the cheapest 15″ MacBook Airs for sale today at Apple.... Read more
May 2024 Apple Education discounts on MacBook...
If you’re a student, teacher, or staff member at any educational institution, you can use your .edu email address when ordering at Apple Education to take up to $300 off the purchase of a new MacBook... Read more
Clearance 16-inch M2 Pro MacBook Pros in stoc...
Apple has clearance 16″ M2 Pro MacBook Pros available in their Certified Refurbished store starting at $2049 and ranging up to $450 off original MSRP. Each model features a new outer case, shipping... Read more
Save $300 at Apple on 14-inch M3 MacBook Pros...
Apple has 14″ M3 MacBook Pros with 16GB of RAM, Certified Refurbished, available for $270-$300 off MSRP. Each model features a new outer case, shipping is free, and an Apple 1-year warranty is... Read more
Apple continues to offer 14-inch M3 MacBook P...
Apple has 14″ M3 MacBook Pros, Certified Refurbished, available starting at only $1359 and ranging up to $270 off MSRP. Each model features a new outer case, shipping is free, and an Apple 1-year... Read more
Apple AirPods Pro with USB-C return to all-ti...
Amazon has Apple’s AirPods Pro with USB-C in stock and on sale for $179.99 including free shipping. Their price is $70 (28%) off MSRP, and it’s currently the lowest price available for new AirPods... Read more
Apple Magic Keyboards for iPads are on sale f...
Amazon has Apple Magic Keyboards for iPads on sale today for up to $70 off MSRP, shipping included: – Magic Keyboard for 10th-generation Apple iPad: $199, save $50 – Magic Keyboard for 11″ iPad Pro/... Read more
Apple’s 13-inch M2 MacBook Airs return to rec...
Apple retailers have 13″ MacBook Airs with M2 CPUs in stock and on sale this weekend starting at only $849 in Space Gray, Silver, Starlight, and Midnight colors. These are the lowest prices currently... Read more

Jobs Board

Liquor Stock Clerk - S. *Apple* St. - Idaho...
Liquor Stock Clerk - S. Apple St. Boise Posting Begin Date: 2023/10/10 Posting End Date: 2024/10/14 Category: Retail Sub Category: Customer Service Work Type: Part Read more
*Apple* App Developer - Datrose (United Stat...
…year experiencein programming and have computer knowledge with SWIFT. Job Responsibilites: Apple App Developer is expected to support essential tasks for the RxASL Read more
Omnichannel Associate - *Apple* Blossom Mal...
Omnichannel Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Operations Associate - *Apple* Blossom Mall...
Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Cashier - *Apple* Blossom Mall - JCPenney (...
Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Mall Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.