-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspeech.py
28 lines (23 loc) · 862 Bytes
/
speech.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
import pyttsx3
import re
class Speech():
def __init__(self) -> None:
self.voice = pyttsx3.init()
self.voice.setProperty("voice", "com.apple.voice.compact.en-US.Samantha")
self.__current_sentence = ""
def speak(self, text):
self.stash(text)
if re.search("[\.\!\?\\n]", text):
self.say(self.__current_sentence.strip())
self.__current_sentence = ""
elif len(self.__current_sentence) > 10_000:
print("The speech buffer is too long. Flushing.")
self.say(self.__current_sentence.strip())
self.__current_sentence = ""
def stash(self, text):
self.__current_sentence += text
def say(self, text):
if len(text) > 0:
self.voice.say(text)
self.voice.runAndWait()
self.voice.stop()