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.
Pausing Speech
When you start synthesizing speech, you may need a way to stop speech that is being generated. For example, your application might support a Stop Speech menu command to let users stop speech when they want to. Also, you should usually stop speech when you receive a suspend event. You can useStopSpeechto stop speech immediately, or you can useStopSpeechAtto choose exactly where you want speech stopped. You can also stop speech temporarily and then resume it again using thePauseSpeechAtandContinueSpeechfunctions. Listing 4-5 shows how you might do this.Listing 4-5 Pausing and continuing speech production
FUNCTION MyPauseAndContinueSpeech: OSErr; VAR myErr, myErr2: OSErr; myStr: Str255; BEGIN gChannel := NIL; myStr := 'Hold the mouse button down to test pause speech at immediate.'; myErr := NewSpeechChannel(NIL, gChannel); {open speech channel} IF myErr = noErr THEN BEGIN {speak some text} myErr := SpeakText(gChannel, @myStr[1], Length(myStr)); WHILE (SpeechBusy <> 0) DO {wait for speech to finish} IF (Button) THEN BEGIN {stop speech immediately} myErr := PauseSpeechAt(gChannel, kImmediate); IF myErr = noErr THEN WHILE (Button) DO {while mouse button is down, do nothing} BEGIN END; {on mouse up, resume speaking} myErr := ContinueSpeech(gChannel); END; IF gChannel <> NIL THEN {dispose of channel} myErr2 := DisposeSpeechChannel(gChannel); END; IF myErr = noErr THEN MyPauseAndContinueSpeech := myErr2 ELSE MyPauseAndContinueSpeech := myErr; END;TheMyPauseAndContinueSpeechfunction defined in Listing 4-5 begins by allocating a speech channel using the default system voice. It then begins to speak some text.MyPauseAndContinueSpeechuses a busy loop to allow the speech to be completely spoken before finishing the subroutine. Then, when the designated action occurs, in this case the mouse button being depressed by a user,MyPauseAndContinueSpeechcallsPauseSpeechAtwith the currently active channel and a constant that defines where to stop the speech. This example uses the constantkImmediateto indicate that the speech should cease wherever it us currently being processed by the synthesizer. There are also constants that define the end of a word and the end of a sentence as appropriate stopping places.When the mouse button is released,
MyPauseAndContinueSpeechcalls theContinueSpeechfunction with the variable identifying the paused speech channel. When paused immediately, the synthesizer resumes speaking at the beginning of the word that was interrupted. While the speech is being generated,MyPauseAndContinueSpeechcontinues to callSpeechBusyto determine if the channel is still being used to process speech. When the channel is no longer busy,MyPauseAndContinueSpeechcallsDisposeSpeechChannelto release the memory used by the speech channel.