| 1 | from pyobjus import autoclass, objc_str |
| 2 | from pyobjus.dylib_manager import load_framework |
| 3 | |
| 4 | from plyer.facades import TTS |
| 5 | |
| 6 | load_framework('/System/Library/Frameworks/AVFoundation.framework') |
| 7 | AVSpeechUtterance = autoclass('AVSpeechUtterance') |
| 8 | AVSpeechSynthesizer = autoclass('AVSpeechSynthesizer') |
| 9 | AVSpeechSynthesisVoice = autoclass('AVSpeechSynthesisVoice') |
| 10 | |
| 11 | |
| 12 | class iOSTextToSpeech(TTS): |
| 13 | def __init__(self): |
| 14 | super().__init__() |
| 15 | self.synth = AVSpeechSynthesizer.alloc().init() |
| 16 | self.voice = None |
| 17 | |
| 18 | def _set_locale(self, locale="en-US"): |
| 19 | self.voice = AVSpeechSynthesisVoice.voiceWithLanguage_( |
| 20 | objc_str(locale) |
| 21 | ) |
| 22 | |
| 23 | def _speak(self, **kwargs): |
| 24 | message = kwargs.get('message') |
| 25 | |
| 26 | if not self.voice: |
| 27 | self._set_locale() |
| 28 | |
| 29 | utterance = \ |
| 30 | AVSpeechUtterance.speechUtteranceWithString_(objc_str(message)) |
| 31 | |
| 32 | utterance.voice = self.voice |
| 33 | self.synth.speakUtterance_(utterance) |
| 34 | |
| 35 | |
| 36 | def instance(): |
| 37 | return iOSTextToSpeech() |