Previous Book Contents Book Index Next

Inside Macintosh: Sound /
Chapter 4 - Speech Manager / Using the Speech Manager


Legacy Documentclose button

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 use StopSpeech to stop speech immediately, or you can use StopSpeechAt to choose exactly where you want speech stopped. You can also stop speech temporarily and then resume it again using the PauseSpeechAt and ContinueSpeech functions. 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;
The MyPauseAndContinueSpeech function defined in Listing 4-5 begins by allocating a speech channel using the default system voice. It then begins to speak some text. MyPauseAndContinueSpeech uses 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, MyPauseAndContinueSpeech calls PauseSpeechAt with the currently active channel and a constant that defines where to stop the speech. This example uses the constant kImmediate to 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, MyPauseAndContinueSpeech calls the ContinueSpeech function 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, MyPauseAndContinueSpeech continues to call SpeechBusy to determine if the channel is still being used to process speech. When the channel is no longer busy, MyPauseAndContinueSpeech calls DisposeSpeechChannel to release the memory used by the speech channel.


Previous Book Contents Book Index Next

© Apple Computer, Inc.
2 JUL 1996