summaryrefslogtreecommitdiff
path: root/external/plyer/platforms/ios/tts.py
blob: a711483e1faff98279156283f23b03c3791e3579 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from pyobjus import autoclass, objc_str
from pyobjus.dylib_manager import load_framework

from plyer.facades import TTS

load_framework('/System/Library/Frameworks/AVFoundation.framework')
AVSpeechUtterance = autoclass('AVSpeechUtterance')
AVSpeechSynthesizer = autoclass('AVSpeechSynthesizer')
AVSpeechSynthesisVoice = autoclass('AVSpeechSynthesisVoice')


class iOSTextToSpeech(TTS):
    def __init__(self):
        super(iOSTextToSpeech, self).__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')

        if(not self.voice):
            self._set_locale()

        utterance = \
            AVSpeechUtterance.speechUtteranceWithString_(objc_str(message))

        utterance.voice = self.voice
        self.synth.speakUtterance_(utterance)


def instance():
    return iOSTextToSpeech()