Important: Inside Macintosh: Sound is deprecated as of Mac OS X v10.5. For new audio development in Mac OS X, use Core Audio. See the Audio page in the ADC Reference Library.
Creating, Using, and Disposing of a Speech Channel
To take advantage of most of the Speech Manager's capabilities, you must pass a speech channel to Speech Manager functions. You use theNewSpeechChannelfunction to create a speech channel. After you are done using a speech channel, you must dispose of it by using theDisposeSpeechChannelfunction. Listing 4-2 shows how to create a speech channel, start speaking text with theSpeakTextfunction, stop speaking text with theStopSpeechfunction, and then dispose of the speech channel when the speaking is finished.Listing 4-2 Speaking text with a speech channel
FUNCTION MyUseSpeechChannel: OSErr; VAR myErr: OSErr; myErr2: OSErr; myStr: Str255; {text to be spoken} BEGIN myStr := 'Hold the mouse button down to stop speech.'; myErr := NewSpeechChannel(NIL, gChannel); {create the channel} IF (myErr = noErr) THEN BEGIN {speak the string} myErr := SpeakText(gChannel, @myStr[1], Length(myStr)); WHILE (SpeechBusy <> 0) DO {wait until speaking is done} BEGIN IF (Button) THEN myErr := StopSpeech(gChannel); {stop speech at mouse down} END; IF (gChannel <> NIL) THEN myErr2 := DisposeSpeechChannel(gChannel);{get rid of channel} END; IF (myErr = noErr) THEN MyUseSpeechChannel := myErr2 ELSE MyUseSpeechChannel := myErr; END;TheMyUseSpeechChannelfunction defined in Listing 4-2 creates a default speech channel using the default system voice. You passNILin the first parameter to use the system default voice. You must also pass a global variable toNewSpeechChannelin which is returned a valid speech channel. Once the channel exists, then you can use theSpeakTextfunction to generate speech. To generate synthesized speech, you pass in the channel allocated byNewSpeechChannelin the first parameter, and then you pass a pointer to the text that you want to speak as well as the length of the text that you want the Speech Manager to attempt to speak. That is, you can pass a pointer to a buffer of text that is 500 bytes long, but specify that only the first 10 bytes get spoken. ThenMyUseSpeechChanneluses theSpeechBusyfunction in aWHILEloop to allow the text to be completely spoken before disposing of the channel.When the designated action to stop the speaking occurs, which in this example is the user pressing the mouse button,
MyUseSpeechChannelhalts speech production. In this case, theStopSpeechfunction stops the speech immediately (as soon as the synthesizer can). You need to passStopSpeechthe variable that identifies the channel on which the speech is currently being synthesized. If you want to have more control over when the speech is stopped, you can use theStopSpeechAtfunction, which allows you to stop speech immediately, at the end of a word, or at the end of a sentence. See the description of theStopSpeechAtfunction on page 4-60 for more information.Once you are done using the speech channel that was created with
NewSpeechChannel, you must dispose of it. TheMyUseSpeechChannelfunction callsDisposeSpeechChannelwith the global variable that identifies the channel currently in use.