7

I am writing an app that includes text-to-speech using AVSpeechSynthesizer. The code for generating the utterance and using the speech synthesizer has been working fine.

let utterance = AVSpeechUtterance(string: text)
utterance.voice = currentVoice
speechSynthesizer.speak(utterance)

Now with iOS 11, I want to match the voice to the one selected by the user in the phone's Settings app, but I do not see any way to get that setting.

I have tried getting the list of installed voices and looking for one that has a quality of .enhanced, but sometimes there is no enhanced voice installed, and even when there is, it may or may not be the voice selected by the user in the Settings app.

static var enhanced: AVSpeechSynthesisVoice? {
    for voice in AVSpeechSynthesisVoice.speechVoices() {
        if voice.quality == .enhanced {
            return voice
        }
    }

    return nil
}

The questions are twofold:

  1. How can I determine which voice has been selected by the user in the Setting app?
  2. Why on some iOS 11 phones that are using the new Siri voice am I not finding an "enhanced" voice installed?

enter image description here

picciano
  • 22,341
  • 9
  • 69
  • 82

3 Answers3

7

I suppose if there was a method available for selecting the same voice as in the Settings app, it'd be shown on the documentation for class AVSpeechSynthesisVoice under the Finding Voices topic. Jumping to the definition in code of AVSpeechSynthesisVoice, I couldn’t find any different methods to retrieve voices.

Here's my workaround on getting an enhanced voice over for the app I am working on:

Enhanced versions of voices are probably not present in new iOS devices by default in order to save storage. Iterating thru available voices on my brand new iPhone, I only found Default quality voices such as: [AVSpeechSynthesisVoice 0x1c4e11cf0] Language: en-US, Name: Samantha, Quality: Default [com.apple.ttsbundle.Samantha-compact]

I found this article on how to enable additional voice over voices and downloaded the one named “Samantha (Enhanced)” among them. Checking the list of available voices again, I noticed the following addition: [AVSpeechSynthesisVoice 0x1c4c03060] Language: en-US, Name: Samantha (Enhanced), Quality: Enhanced [com.apple.ttsbundle.Samantha-premium]

As of now I was able to select an enhanced language on Xcode. Given that the AVSpeechSynthesisVoice.currentLanguageCode() method exposes the currently selected language, ran the following code to make a selection of the first enhanced voice I could find. If no enhanced version was available I’d just pick the available default (the code below is for a VoiceOver custom class I am creating to handle all speeches in my app. The piece below updates its voice variable).

var voice: AVSpeechSynthesisVoice!

for availableVoice in AVSpeechSynthesisVoice.speechVoices(){
        if ((availableVoice.language == AVSpeechSynthesisVoice.currentLanguageCode()) &&
            (availableVoice.quality == AVSpeechSynthesisVoiceQuality.enhanced)){ // If you have found the enhanced version of the currently selected language voice amongst your available voices... Usually there's only one selected.
            self.voice = availableVoice
            print("\(availableVoice.name) selected as voice for uttering speeches. Quality: \(availableVoice.quality.rawValue)")
        }
}
if let selectedVoice = self.voice { // if sucessfully unwrapped, the previous routine was able to identify one of the enhanced voices
        print("The following voice identifier has been loaded: ",selectedVoice.identifier)
} else {
        self.voice = AVSpeechSynthesisVoice(language: AVSpeechSynthesisVoice.currentLanguageCode()) // load any of the voices that matches the current language selection for the device in case no enhanced voice has been found.

}

I am also hoping Apple will expose a method to directly load the selected language, but I hope this work around can serve you in the meantime. I guess Siri’s enhanced voice is downloaded on the go, so maybe this is the reason it takes so long to answer my voice commands :)

Best regards.

Andre Guerra
  • 1,117
  • 1
  • 9
  • 18
  • It looks like you can access any of the "named" voices, and even use their premium/Enhanced versions, if the user has downloaded them to the device. The Siri voice (I think it's "Nora" on macOS, but *NOT* on iOS) still seems to be off-limits. When I select her as my default voice in Settings and ask for an `@"en-US"` voice, I get the old, computery Siri. Bummer. – mbm29414 Jan 11 '18 at 19:49
  • 1
    I wrote http://openradar.appspot.com/42656565 to access Siri's voice. Please dupe. – steipete Jul 27 '18 at 10:39
2

It looks like the new Siri voice in iOS 11 isn't part of the AVSpeechSynthesis API, and isn't available to developers.

In macOS 10.13 High Sierra (which also gets the new voice), there seems to be a new SiriTTS framework that's probably related to this functionality, but it's in PrivateFrameworks so it doesn't have a developer API.

rickster
  • 124,678
  • 26
  • 272
  • 326
1

I'll try to provide a more detailed answer. AVSpeechSynthesizer cannot use the Siri voice. Apple has locked this voice to ensure privacy as the malicious app could impersonate Siri and get private information that way.

Apple hasn't changed this for years, but there is ongoing initiative regarding this. We already know that there is a solution to access privacy sensitive features in the iOS using the permissions, and there is no reason why Siri voice couldn't be accessed with user permission. You may vote for this to happen using this petition and with some hope Apple may implement that in the future: https://www.change.org/p/apple-apple-please-allow-3rd-party-apps-to-use-siri-voices-for-improved-accessibility

Ivan Ičin
  • 9,672
  • 5
  • 36
  • 57