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.

Working With Different Voices

When you work with speech channels, you can set a voice for a particular channel. When you set a voice, you may want to filter out certain of its characteristics in order to identify the one you want. For example, in an educational software application for elementary school students, you may want to use only children's voices. In order to choose the voice you want, you get a voice description record that contains information about a voice such as the size of the voice, the name of the voice, the age and gender of the voice, and the synthesizer with which it works. You can get the number of available voices using the CountVoices function. You can cycle through the available voices and identify the one you want to use by using the GetIndVoice function. Then you fill out a voice description record using the GetVoiceDescription function. Listing 4-3 shows how to get identifying information about a voice.

Listing 4-3 Getting a description of a voice

FUNCTION MyInstallBoysVoice: OSErr;
VAR
   myErr:         OSErr;
   myIndex:       Integer;
   myNumVoices:   Integer;
   myVoice:       VoiceSpec;
   myFound:       VoiceSpec;
   myInfo:        VoiceDescription;
BEGIN
   myFound := NIL;
   myErr := CountVoices(myNumVoices);           {count voices}
   IF myErr = noErr THEN
   BEGIN
      FOR myIndex := 0 to myNumVoices DO        {loop through all voices}
      BEGIN
         myErr := GetIndVoice(myIndex, @myVoice);
         IF myErr = noErr THEN
         BEGIN
            myErr := GetVoiceDescription(@myVoice, @myInfo, sizeof(myInfo));
            IF myErr = noErr THEN               {check if a boy's voice}
               IF (myVoice.age < 16) AND (myVoice.gender = kMale) THEN
                  myFound := myVoice;
         END;
      END; {FOR}
      IF myFound <> NIL THEN                    {install boy's voice}
         myErr := NewSpeechChannel(@myFound, gChannel);
   END;
   MyInstallBoysVoice := myErr;                 {return result code}
END;
The MyGetVoiceInfo function checks to see how many voices are available. Once you have identified the list of available voices, you can index through the voices to select one about which you want to get information. You pass the number of the voice index in the first parameter of the GetIndVoice function. (This number cannot be larger than the number of voices.) GetIndVoice returns a voice specification record in the location specified in the second parameter-- in this case, in the location of the pointer @myVoice. This sample cycles through the available voices looking for a male child's voice.

The voice specification record contains two identifiers: the creator identification of the required synthesizer and the voice identification of the voice.In order to get specific information about the voice you want to use, you need to call the GetVoiceDescription function. You need to pass a pointer to the voice specification record in the first parameter of the GetVoiceDescription function. GetVoiceDescription returns the voice description record in the location pointed to in the second parameter, @info. The voice description record contains information about the voice such as its age or gender.

To specify which voice you want to use, you pass a pointer to the voice specification record as the first parameter to NewSpeechChannel. In this case, when the male child's voice is identified, it's voice specification record is passed to NewSpeechChannel, which allocates a channel with the specified voice. Note that this sample code contains limited error checking.


Previous Book Contents Book Index Next

© Apple Computer, Inc.
2 JUL 1996