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

Whitethorn Games combines two completely...
If you have ever gone fishing then you know that it is a lesson in patience, sitting around waiting for a bite that may never come. Well, that's because you have been doing it wrong, since as Whitehorn Games now demonstrates in new release Skate... | Read more »
Call of Duty Warzone is a Waiting Simula...
It's always fun when a splashy multiplayer game comes to mobile because they are few and far between, so I was excited to see the notification about Call of Duty: Warzone Mobile (finally) launching last week and wanted to try it out. As someone who... | Read more »
Albion Online introduces some massive ne...
Sandbox Interactive has announced an upcoming update to its flagship MMORPG Albion Online, containing massive updates to its existing guild Vs guild systems. Someone clearly rewatched the Helms Deep battle in Lord of the Rings and spent the next... | Read more »
Chucklefish announces launch date of the...
Chucklefish, the indie London-based team we probably all know from developing Terraria or their stint publishing Stardew Valley, has revealed the mobile release date for roguelike deck-builder Wildfrost. Developed by Gaziter and Deadpan Games, the... | Read more »
Netmarble opens pre-registration for act...
It has been close to three years since Netmarble announced they would be adapting the smash series Solo Leveling into a video game, and at last, they have announced the opening of pre-orders for Solo Leveling: Arise. [Read more] | Read more »
PUBG Mobile celebrates sixth anniversary...
For the past six years, PUBG Mobile has been one of the most popular shooters you can play in the palm of your hand, and Krafton is celebrating this milestone and many years of ups by teaming up with hit music man JVKE to create a special song for... | Read more »
ASTRA: Knights of Veda refuse to pump th...
In perhaps the most recent example of being incredibly eager, ASTRA: Knights of Veda has dropped its second collaboration with South Korean boyband Seventeen, named so as it consists of exactly thirteen members and a video collaboration with Lee... | Read more »
Collect all your cats and caterpillars a...
If you are growing tired of trying to build a town with your phone by using it as a tiny, ineffectual shover then fear no longer, as Independent Arts Software has announced the upcoming release of Construction Simulator 4, from the critically... | Read more »
Backbone complete its lineup of 2nd Gene...
With all the ports of big AAA games that have been coming to mobile, it is becoming more convenient than ever to own a good controller, and to help with this Backbone has announced the completion of their 2nd generation product lineup with their... | Read more »
Zenless Zone Zero opens entries for its...
miHoYo, aka HoYoverse, has become such a big name in mobile gaming that it's hard to believe that arguably their flagship title, Genshin Impact, is only three and a half years old. Now, they continue the road to the next title in their world, with... | Read more »

Price Scanner via MacPrices.net

B&H has Apple’s 13-inch M2 MacBook Airs o...
B&H Photo has 13″ MacBook Airs with M2 CPUs and 256GB of storage in stock and on sale for up to $150 off Apple’s new MSRP, starting at only $849. Free 1-2 day delivery is available to most US... Read more
M2 Mac minis on sale for $100-$200 off MSRP,...
B&H Photo has Apple’s M2-powered Mac minis back in stock and on sale today for $100-$200 off MSRP. Free 1-2 day shipping is available for most US addresses: – Mac mini M2/256GB SSD: $499, save $... Read more
Mac Studios with M2 Max and M2 Ultra CPUs on...
B&H Photo has standard-configuration Mac Studios with Apple’s M2 Max & Ultra CPUs in stock today and on Easter sale for $200 off MSRP. Their prices are the lowest available for these models... Read more
Deal Alert! B&H Photo has Apple’s 14-inch...
B&H Photo has new Gray and Black 14″ M3, M3 Pro, and M3 Max MacBook Pros on sale for $200-$300 off MSRP, starting at only $1399. B&H offers free 1-2 day delivery to most US addresses: – 14″ 8... Read more
Department Of Justice Sets Sights On Apple In...
NEWS – The ball has finally dropped on the big Apple. The ball (metaphorically speaking) — an antitrust lawsuit filed in the U.S. on March 21 by the Department of Justice (DOJ) — came down following... Read more
New 13-inch M3 MacBook Air on sale for $999,...
Amazon has Apple’s new 13″ M3 MacBook Air on sale for $100 off MSRP for the first time, now just $999 shipped. Shipping is free: – 13″ MacBook Air (8GB RAM/256GB SSD/Space Gray): $999 $100 off MSRP... Read more
Amazon has Apple’s 9th-generation WiFi iPads...
Amazon has Apple’s 9th generation 10.2″ WiFi iPads on sale for $80-$100 off MSRP, starting only $249. Their prices are the lowest available for new iPads anywhere: – 10″ 64GB WiFi iPad (Space Gray or... Read more
Discounted 14-inch M3 MacBook Pros with 16GB...
Apple retailer Expercom has 14″ MacBook Pros with M3 CPUs and 16GB of standard memory discounted by up to $120 off Apple’s MSRP: – 14″ M3 MacBook Pro (16GB RAM/256GB SSD): $1691.06 $108 off MSRP – 14... Read more
Clearance 15-inch M2 MacBook Airs on sale for...
B&H Photo has Apple’s 15″ MacBook Airs with M2 CPUs (8GB RAM/256GB SSD) in stock today and on clearance sale for $999 in all four colors. Free 1-2 delivery is available to most US addresses.... Read more
Clearance 13-inch M1 MacBook Airs drop to onl...
B&H has Apple’s base 13″ M1 MacBook Air (Space Gray, Silver, & Gold) in stock and on clearance sale today for $300 off MSRP, only $699. Free 1-2 day shipping is available to most addresses in... Read more

Jobs Board

Medical Assistant - Surgical Oncology- *Apple...
Medical Assistant - Surgical Oncology- Apple Hill Location: WellSpan Medical Group, York, PA Schedule: Full Time Sign-On Bonus Eligible Remote/Hybrid Regular Apply 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
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
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
Business Analyst | *Apple* Pay - Banco Popu...
Business Analyst | Apple PayApply now " Apply now + Apply Now + Start applying with LinkedIn Start + Please wait Date:Mar 19, 2024 Location: San Juan-Cupey, PR Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.