openCom-Companion/sbapp/plyer/platforms/ios/tts.py

38 lines
1.0 KiB
Python
Raw Permalink Normal View History

2022-09-16 18:07:57 +02:00
from pyobjus import autoclass, objc_str
from pyobjus.dylib_manager import load_framework
2024-06-02 18:31:58 +02:00
from sbapp.plyer.facades import TTS
2022-09-16 18:07:57 +02:00
load_framework('/System/Library/Frameworks/AVFoundation.framework')
AVSpeechUtterance = autoclass('AVSpeechUtterance')
AVSpeechSynthesizer = autoclass('AVSpeechSynthesizer')
AVSpeechSynthesisVoice = autoclass('AVSpeechSynthesisVoice')
class iOSTextToSpeech(TTS):
def __init__(self):
super().__init__()
self.synth = AVSpeechSynthesizer.alloc().init()
self.voice = None
def _set_locale(self, locale="en-US"):
self.voice = AVSpeechSynthesisVoice.voiceWithLanguage_(
objc_str(locale)
)
def _speak(self, **kwargs):
message = kwargs.get('message')
2024-06-02 18:31:58 +02:00
if not self.voice:
2022-09-16 18:07:57 +02:00
self._set_locale()
utterance = \
AVSpeechUtterance.speechUtteranceWithString_(objc_str(message))
utterance.voice = self.voice
self.synth.speakUtterance_(utterance)
def instance():
return iOSTextToSpeech()