Values for AVSpeechSynthesisIPANotationAttribute?

I'm trying to figure out how to use AVSpeechUtterance's attributed-string initializer with the AVSpeechSynthesisIPANotationAttribute attribute. These were added in iOS 10 but not fully documented.

I'm doing this in an iOS playground in Xcode 8.3:


import AVFoundation
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true

let str = "North Lake Street"
let utterance = AVSpeechUtterance(string: str)
let synthesizer = AVSpeechSynthesizer()
synthesizer.speak(utterance)

let attrs: [String: Any] = [AVSpeechSynthesisIPANotationAttribute: NSNumber(value: 0)]
let ipa = "ˈnɔrθ ˈlek ˈstrit"
let ipaAttrib = NSMutableAttributedString(string: ipa, attributes: attrs)
let ipaUtterance = AVSpeechUtterance(attributedString: ipaAttrib)
synthesizer.speak(ipaUtterance)

I was hoping that the second version would sound similar to the first, but it really doesn't sound very good.


Is there any guidance on what value to give the AVSpeechSynthesisIPANotationAttribute attribute? I've experimented with numbers and strings like "en" but haven't gotten good results.

Answered by OOPer in 246361022

`AVSpeechSynthesisIPANotationAttribute` has a simple header doc.

//NSString, containing International Phonetic Alphabet (IPA) symbols. Controls pronunciation of a certain word or phrase, e.g. a proper name.


So, its value needs to be a String, and the attribute needs to be added to a certain word or phrase.


You can write something like this.

let str = "North Lake Street"
let synthesizer = AVSpeechSynthesizer()
let ipaAttrib = NSMutableAttributedString(string: str)
ipaAttrib.addAttribute(AVSpeechSynthesisIPANotationAttribute, value: "laki", range: NSRange(6..<10))
let ipaUtterance = AVSpeechUtterance(attributedString: ipaAttrib)
synthesizer.speak(ipaUtterance)
Accepted Answer

`AVSpeechSynthesisIPANotationAttribute` has a simple header doc.

//NSString, containing International Phonetic Alphabet (IPA) symbols. Controls pronunciation of a certain word or phrase, e.g. a proper name.


So, its value needs to be a String, and the attribute needs to be added to a certain word or phrase.


You can write something like this.

let str = "North Lake Street"
let synthesizer = AVSpeechSynthesizer()
let ipaAttrib = NSMutableAttributedString(string: str)
ipaAttrib.addAttribute(AVSpeechSynthesisIPANotationAttribute, value: "laki", range: NSRange(6..<10))
let ipaUtterance = AVSpeechUtterance(attributedString: ipaAttrib)
synthesizer.speak(ipaUtterance)

Thanks, @OOPer, the code you included helped!


It's an interesting point - it appears that you need to apply the IPA attribute on a word-by-word basis. If you have a long IPA string and apply it to a whole sentence, it appears that it gets ignored.

it appears that you need to apply the IPA attribute on a word-by-word basis.


Yes.


Maybe the set of phrase is very limited and held somewhere in the AVSpeechSynthesis framework. It may contain only some phrases (defined for each language, I believe) which should be pronounced as a single word.


Though, it's just my guess and I could not have found any example of phrase till now.

Values for AVSpeechSynthesisIPANotationAttribute?
 
 
Q