Search code examples
objective-ccocoatext-to-speechspeech-synthesis

Mac OS X Text-To-Speech Genders


I'm making an application, in which I am trying to get all the different voices in Mac OS X, and then sort them by gender. I've created three mutable arrays to put the voices of each gender (Male, Female, Novelty) in, and I'm using enumeration to go through each one and put it in the correct array. Unfortunately, It's not working. All but the novelty arrays come up empty, and the novelty array only has one voice, Zarvox. Does anybody see what I'm doing wrong? I've posted the code below:

NSArray* voices = [NSSpeechSynthesizer availableVoices]; 
for(NSString* x in voices){

    NSDictionary* voiceInfo = [NSSpeechSynthesizer attributesForVoice:x];

    NSString* voiceName = [voiceInfo objectForKey:NSVoiceName];
    NSString* voiceGender = [voiceInfo objectForKey:NSVoiceGender];


    maleVoices = [[NSMutableArray alloc] init];
    femaleVoices = [[NSMutableArray alloc] init];
    noveltyVoices = [[NSMutableArray alloc] init];

    if (voiceGender == NSVoiceGenderMale){
        [maleVoices addObject:voiceName];

    } else if (voiceGender == NSVoiceGenderFemale) {
        [femaleVoices addObject:voiceName];
    } else {

        [noveltyVoices addObject:voiceName];
    }
}

Solution

  • Allocate maleVoices, femaleVoices, and noveltyVoices outside of the for loop. You're just creating a new, empty array with each iteration of the loop.