Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AVSpeechSynthesizer High Quality Voices

Is it possible to use the enhanced/high quality voices (Alex in the U.S.) with the speech synthesizer? I have downloaded the voices but find no way to tell the synthesizer to use it rather than the default voice.

Since voices are generally selected by BCP-47 codes and there is only on for US English, it appears there is no way to further differentiate voices. Am I missing something? (One would think Apple might have considered a need for different dialects, but I am not seeing it).

TIA.

like image 835
RegularExpression Avatar asked Nov 09 '22 09:11

RegularExpression


1 Answers

Yes, possible to pick from the 2 that seem to be available on my system, like this:

class Speak {

    let voices = AVSpeechSynthesisVoice.speechVoices()
    let voiceSynth = AVSpeechSynthesizer()
    var voiceToUse: AVSpeechSynthesisVoice?

  init(){
    for voice in voices {
      if voice.name == "Samantha (Enhanced)"  && voice.quality == .enhanced {
        voiceToUse = voice
      }
    }    
  }

    func sayThis(_ phrase: String){
      let utterance = AVSpeechUtterance(string: phrase)
          utterance.voice = voiceToUse
          utterance.rate = 0.5

        voiceSynth.speak(utterance) 
    }
}

Then, somewhere in your app, do something like this:

let voice = Speak()
voice.sayThis("I'm speaking better Seppo, now!")
like image 183
Confused Avatar answered Nov 15 '22 13:11

Confused