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

Fresh From the Land Down Under – The Tou...
After a two week hiatus, we are back with another episode of The TouchArcade Show. Eli is fresh off his trip to Australia, which according to him is very similar to America but more upside down. Also kangaroos all over. Other topics this week... | Read more »
TouchArcade Game of the Week: ‘Dungeon T...
I’m a little conflicted on this week’s pick. Pretty much everyone knows the legend of Dungeon Raid, the match-3 RPG hybrid that took the world by storm way back in 2011. Everyone at the time was obsessed with it, but for whatever reason the... | Read more »
SwitchArcade Round-Up: Reviews Featuring...
Hello gentle readers, and welcome to the SwitchArcade Round-Up for July 19th, 2024. In today’s article, we finish up the week with the unusual appearance of a review. I’ve spent my time with Hot Lap Racing, and I’m ready to give my verdict. After... | Read more »
Draknek Interview: Alan Hazelden on Thin...
Ever since I played my first release from Draknek & Friends years ago, I knew I wanted to sit down with Alan Hazelden and chat about the team, puzzle games, and much more. | Read more »
The Latest ‘Marvel Snap’ OTA Update Buff...
I don’t know about all of you, my fellow Marvel Snap (Free) players, but these days when I see a balance update I find myself clenching my… teeth and bracing for the impact to my decks. They’ve been pretty spicy of late, after all. How will the... | Read more »
‘Honkai Star Rail’ Version 2.4 “Finest D...
HoYoverse just announced the Honkai Star Rail (Free) version 2.4 “Finest Duel Under the Pristine Blue" update alongside a surprising collaboration. Honkai Star Rail 2.4 follows the 2.3 “Farewell, Penacony" update. Read about that here. | Read more »
‘Vampire Survivors+’ on Apple Arcade Wil...
Earlier this month, Apple revealed that poncle’s excellent Vampire Survivors+ () would be heading to Apple Arcade as a new App Store Great. I reached out to poncle to check in on the DLC for Vampire Survivors+ because only the first two DLCs were... | Read more »
Homerun Clash 2: Legends Derby opens for...
Since launching in 2018, Homerun Clash has performed admirably for HAEGIN, racking up 12 million players all eager to prove they could be the next baseball champions. Well, the title will soon be up for grabs again, as Homerun Clash 2: Legends... | Read more »
‘Neverness to Everness’ Is a Free To Pla...
Perfect World Games and Hotta Studio (Tower of Fantasy) announced a new free to play open world RPG in the form of Neverness to Everness a few days ago (via Gematsu). Neverness to Everness has an urban setting, and the two reveal trailers for it... | Read more »
Meditative Puzzler ‘Ouros’ Coming to iOS...
Ouros is a mediative puzzle game from developer Michael Kamm that launched on PC just a couple of months back, and today it has been revealed that the title is now heading to iOS and Android devices next month. Which is good news I say because this... | Read more »

Price Scanner via MacPrices.net

Amazon is still selling 16-inch MacBook Pros...
Prime Day in July is over, but Amazon is still selling 16-inch Apple MacBook Pros for $500-$600 off MSRP. Shipping is free. These are the lowest prices available this weekend for new 16″ Apple... Read more
Walmart continues to sell clearance 13-inch M...
Walmart continues to offer clearance, but new, Apple 13″ M1 MacBook Airs (8GB RAM, 256GB SSD) online for $699, $300 off original MSRP, in Space Gray, Silver, and Gold colors. These are new MacBooks... Read more
Apple is offering steep discounts, up to $600...
Apple has standard-configuration 16″ M3 Max MacBook Pros available, Certified Refurbished, starting at $2969 and ranging up to $600 off MSRP. Each model features a new outer case, shipping is free,... Read more
Save up to $480 with these 14-inch M3 Pro/M3...
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
Amazon has clearance 9th-generation WiFi iPad...
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
Apple is offering a $50 discount on 2nd-gener...
Apple has Certified Refurbished White and Midnight HomePods available for $249, Certified Refurbished. That’s $50 off MSRP and the lowest price currently available for a full-size Apple HomePod today... Read more
The latest MacBook Pro sale at Amazon: 16-inc...
Amazon is offering instant discounts on 16″ M3 Pro and 16″ M3 Max MacBook Pros ranging up to $400 off MSRP as part of their early July 4th sale. Shipping is free. These are the lowest prices... Read more
14-inch M3 Pro MacBook Pros with 36GB of RAM...
B&H Photo has 14″ M3 Pro MacBook Pros with 36GB of RAM and 512GB or 1TB SSDs in stock today and on sale for $200 off Apple’s MSRP, each including free 1-2 day shipping: – 14″ M3 Pro MacBook Pro (... Read more
14-inch M3 MacBook Pros with 16GB of RAM on s...
B&H Photo has 14″ M3 MacBook Pros with 16GB of RAM and 512GB or 1TB SSDs in stock today and on sale for $150-$200 off Apple’s MSRP, each including free 1-2 day shipping: – 14″ M3 MacBook Pro (... Read more
Amazon is offering $170-$200 discounts on new...
Amazon is offering a $170-$200 discount on every configuration and color of Apple’s M3-powered 15″ MacBook Airs. Prices start at $1129 for models with 8GB of RAM and 256GB of storage: – 15″ M3... Read more

Jobs Board

*Apple* Systems Engineer - Chenega Corporati...
…LLC,** a **Chenega Professional Services** ' company, is looking for a ** Apple Systems Engineer** to support the Information Technology Operations and Maintenance Read more
Solutions Engineer - *Apple* - SHI (United...
**Job Summary** An Apple Solution Engineer's primary role is tosupport SHI customers in their efforts to select, deploy, and manage Apple operating systems and Read more
*Apple* / Mac Administrator - JAMF Pro - Ame...
Amentum is seeking an ** Apple / Mac Administrator - JAMF Pro** to provide support with the Apple Ecosystem to include hardware and software to join our team and 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.