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

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 »
Summon your guild and prepare for war in...
Netmarble is making some pretty big moves with their latest update for Seven Knights Idle Adventure, with a bunch of interesting additions. Two new heroes enter the battle, there are events and bosses abound, and perhaps most interesting, a huge... | Read more »
Make the passage of time your plaything...
While some of us are still waiting for a chance to get our hands on Ash Prime - yes, don’t remind me I could currently buy him this month I’m barely hanging on - Digital Extremes has announced its next anticipated Prime Form for Warframe. Starting... | Read more »
If you can find it and fit through the d...
The holy trinity of amazing company names have come together, to release their equally amazing and adorable mobile game, Hamster Inn. Published by HyperBeard Games, and co-developed by Mum Not Proud and Little Sasquatch Studios, it's time to... | Read more »
Amikin Survival opens for pre-orders on...
Join me on the wonderful trip down the inspiration rabbit hole; much as Palworld seemingly “borrowed” many aspects from the hit Pokemon franchise, it is time for the heavily armed animal survival to also spawn some illegitimate children as Helio... | Read more »
PUBG Mobile teams up with global phenome...
Since launching in 2019, SpyxFamily has exploded to damn near catastrophic popularity, so it was only a matter of time before a mobile game snapped up a collaboration. Enter PUBG Mobile. Until May 12th, players will be able to collect a host of... | Read more »
Embark into the frozen tundra of certain...
Chucklefish, developers of hit action-adventure sandbox game Starbound and owner of one of the cutest logos in gaming, has released their roguelike deck-builder Wildfrost. Created alongside developers Gaziter and Deadpan Games, Wildfrost will... | Read more »
MoreFun Studios has announced Season 4,...
Tension has escalated in the ever-volatile world of Arena Breakout, as your old pal Randall Fisher and bosses Fred and Perrero continue to lob insults and explosives at each other, bringing us to a new phase of warfare. Season 4, Into The Fog of... | Read more »
Top Mobile Game Discounts
Every day, we pick out a curated list of the best mobile discounts on the App Store and post them here. This list won't be comprehensive, but it every game on it is recommended. Feel free to check out the coverage we did on them in the links below... | Read more »

Price Scanner via MacPrices.net

Free iPhone 15 plus Unlimited service for $60...
Boost Infinite, part of MVNO Boost Mobile using AT&T and T-Mobile’s networks, is offering a free 128GB iPhone 15 for $60 per month including their Unlimited service plan (30GB of premium data).... Read more
$300 off any new iPhone with service at Red P...
Red Pocket Mobile has new Apple iPhones on sale for $300 off MSRP when you switch and open up a new line of service. Red Pocket Mobile is a nationwide MVNO using all the major wireless carrier... Read more
Clearance 13-inch M1 MacBook Airs available a...
Apple has clearance 13″ M1 MacBook Airs, Certified Refurbished, available for $759 for 8-Core CPU/7-Core GPU/256GB models and $929 for 8-Core CPU/8-Core GPU/512GB models. Apple’s one-year warranty is... Read more
Updated Apple MacBook Price Trackers
Our Apple award-winning MacBook Price Trackers are continually updated with the latest information on prices, bundles, and availability for 16″ and 14″ MacBook Pros along with 13″ and 15″ MacBook... Read more
Every model of Apple’s 13-inch M3 MacBook Air...
Best Buy has Apple 13″ MacBook Airs with M3 CPUs in stock and on sale today for $100 off MSRP. Prices start at $999. Their prices are the lowest currently available for new 13″ M3 MacBook Airs among... Read more
Sunday Sale: Apple iPad Magic Keyboards for 1...
Walmart has Apple Magic Keyboards for 12.9″ iPad Pros, in Black, on sale for $150 off MSRP on their online store. Sale price for online orders only, in-store price may vary. Order online and choose... Read more
Apple Watch Ultra 2 now available at Apple fo...
Apple has, for the first time, begun offering Certified Refurbished Apple Watch Ultra 2 models in their online store for $679, or $120 off MSRP. Each Watch includes Apple’s standard one-year warranty... Read more
AT&T has the iPhone 14 on sale for only $...
AT&T has the 128GB Apple iPhone 14 available for only $5.99 per month for new and existing customers when you activate unlimited service and use AT&T’s 36 month installment plan. The fine... Read more
Amazon is offering a $100 discount on every M...
Amazon is offering a $100 instant discount on each configuration of Apple’s new 13″ M3 MacBook Air, in Midnight, this weekend. These are the lowest prices currently available for new 13″ M3 MacBook... Read more
You can save $300-$480 on a 14-inch M3 Pro/Ma...
Apple has 14″ M3 Pro and M3 Max MacBook Pros in stock today and available, Certified Refurbished, starting at $1699 and ranging up to $480 off MSRP. Each model features a new outer case, shipping is... Read more

Jobs Board

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
IT Systems Engineer ( *Apple* Platforms) - S...
IT Systems Engineer ( Apple Platforms) at SpaceX Hawthorne, CA SpaceX was founded under the belief that a future where humanity is out exploring the stars is Read more
*Apple* Systems Administrator - JAMF - Activ...
…**Public Trust/Other Required:** None **Job Family:** Systems Administration **Skills:** Apple Platforms,Computer Servers,Jamf Pro **Experience:** 3 + years of Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.