Last active
October 12, 2022 13:47
Extension to AVSpeechSynthesizer that will speak IPA (International Phonetic Alphabet) strings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import AVFoundation | |
@available(iOS 10.0, *) | |
extension AVSpeechSynthesizer { | |
func speakIPA(_ ipaString: String, voiceIdentifier: String, willSpeak: ((String) -> Void)? = nil) { | |
//Set the audio session to playback to ignore mute switch on device | |
do { | |
try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback, options: [.interruptSpokenAudioAndMixWithOthers, .duckOthers]) | |
} catch { | |
print("Error: \(error.localizedDescription)") | |
} | |
let mutableAttributedString = NSMutableAttributedString(string: ipaString) | |
let range = NSString(string: ipaString).range(of: ipaString) | |
let pronunciationKey = NSAttributedString.Key(rawValue: AVSpeechSynthesisIPANotationAttribute) | |
mutableAttributedString.setAttributes([pronunciationKey: ipaString], range: range) | |
let utterance = AVSpeechUtterance(attributedString: mutableAttributedString) | |
let voice = AVSpeechSynthesisVoice(identifier: voiceIdentifier) | |
utterance.voice = voice | |
// Pausing first is safer and may prevent bugs | |
self.pauseSpeaking(at: .immediate) | |
self.stopSpeaking(at: .immediate) | |
// Run some code just before speaking | |
willSpeak?(utterance.speechString) | |
print("speakIPA: \(ipaString) voice: \(voice?.identifier ?? "?")") | |
self.speak(utterance) | |
} | |
} |
Man, kinda OT but the app I was using this in got lost and now I have to rewrite it.
I gotta keep all my code in a remote now!
I think I had gotten something close enough with a Mexican Spanish voice with just a few overrides for double ll sound.
I still haven’t gotten the energy to rewrite that app, yet.
I appreciate the pointers and will revisit them when I rewrite toki pwnage.
… On Oct 11, 2022, at 12:49 PM, Ryan Lintott ***@***.***> wrote:
@ryanlintott commented on this gist.
The only way to ensure your pronunciations are correct would be to add IPA pronunciations to each word. You need to use IPA symbols (I find this helpful: https://ipa.typeit.org <https://ipa.typeit.org/>) and there will be some combinations that Apple might not pronounce correctly. If Toki Pona is purely phonetic then maybe you could do a substitution swapping letters for IPA symbols. If however it's more like English where letters may sound different depending on the context you'll probably need to translate each word as the rules would be too complex to code. Either way, once you have IPA pronunciations you think are accurate, then I would try testing with voices, finding one that is the closest fit, then testing with words and making any voice-specific adjustments to IPA characters (like I've done with Old English).
When picking IPA symbols I found this app very helpful as you could hear the different sounds:
https://apps.apple.com/app/id869642260
Also this web app was helpful for testing even though it's using non-Apple voices as it's pretty accurate.
http://ipa-reader.xyz <http://ipa-reader.xyz/>
—
Reply to this email directly, view it on GitHub <https://gist.github.com/e1284682216d4e4a90fa474a676fb453#gistcomment-4331749>, or unsubscribe <https://github.com/notifications/unsubscribe-auth/AAL5QCPXAXB2TW4ZADJYWETWCWSC5ANCNFSM5T7LAM4A>.
You are receiving this because you commented.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The only way to ensure your pronunciations are correct would be to add IPA pronunciations to each word. You need to use IPA symbols (I find this helpful: https://ipa.typeit.org) and there will be some combinations that Apple might not pronounce correctly. If Toki Pona is purely phonetic then maybe you could do a substitution swapping letters for IPA symbols. If however it's more like English where letters may sound different depending on the context you'll probably need to translate each word as the rules would be too complex to code. Either way, once you have IPA pronunciations you think are accurate, then I would try testing with voices, finding one that is the closest fit, then testing with words and making any voice-specific adjustments to IPA characters (like I've done with Old English).
When picking IPA symbols I found this app very helpful as you could hear the different sounds:
https://apps.apple.com/app/id869642260
Also this web app was helpful for testing even though it's using non-Apple voices as it's pretty accurate.
http://ipa-reader.xyz