forked from cvpe/Pythonista-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GetSynthesizerAndVoice.py
62 lines (48 loc) · 1.87 KB
/
GetSynthesizerAndVoice.py
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"""
I still wonder why we choose to use ObjC speech instead of the Pythonista speech module.
"""
from objc_util import ObjCClass, ObjCInstance
from typing import Tuple
AVSpeechUtterance = ObjCClass("AVSpeechUtterance")
def get_synthesizer_and_voice(
language: str="ja-JP") -> Tuple[ObjCInstance, ObjCInstance]:
assert isinstance(language, str), (type(language), language)
AVSpeechSynthesizer = ObjCClass("AVSpeechSynthesizer")
AVSpeechSynthesisVoice = ObjCClass("AVSpeechSynthesisVoice")
synthesizer = AVSpeechSynthesizer.new()
for voice in AVSpeechSynthesisVoice.speechVoices():
# print(voice, voice.description())
if language in str(voice.description()):
return synthesizer, voice
raise ValueError(f"No voice found for {language}")
def my_say(text: str="Hello", language: str="en-US", rate: float=0.5) -> None:
try:
synthesizer, voice = get_synthesizer_and_voice(language)
except ValueError as e:
print(e)
synthesizer, voice = get_synthesizer_and_voice("en-US")
utterance = AVSpeechUtterance.speechUtteranceWithString_(text)
#the value that sounds good apparantly depends on ios version
utterance.rate = rate
utterance.useCompactVoice = False
utterance.voice = voice
synthesizer.speakUtterance_(utterance)
del utterance
if __name__ == "__main__":
my_say("Are you talkin' to me?")
import time
time.sleep(2)
my_say("Sprichst du mit mir?", "de-DE")
time.sleep(2)
my_say("Sprichst du mit mir?", "junk")
time.sleep(2)
my_say("Are you talkin' to me?", rate=0.2)
time.sleep(4)
import speech
speech.say("Are you talkin' to me?")
while speech.is_speaking():
time.sleep(0.1)
speech.say("Sprichst du mit mir?", "de-DE")
while speech.is_speaking():
time.sleep(0.1)
speech.say("Are you talkin' to me?", "", 0.2)