Skip to content

Commit

Permalink
implement multi-language speaker
Browse files Browse the repository at this point in the history
  • Loading branch information
DGrothe-PhD committed Oct 7, 2024
1 parent fdb1062 commit 9cb6106
Showing 1 changed file with 45 additions and 2 deletions.
47 changes: 45 additions & 2 deletions speakerSetup.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ class SpeakerStatus:
engine = None
speak = None
#
default_voice_id = None
current_lang = "German"
voices_dict = {}
#
@staticmethod
def is_windows_platform() -> bool:
"""True if system is Windows"""
Expand All @@ -35,8 +39,11 @@ def initializePyTTSSpeaker(cls) -> bool:
"""Tries to initialize py3-tts speaker"""
try:
cls.engine = pyttsx3.init()
voices = cls.engine.getProperty('voices')
cls.engine.setProperty('voice', voices[0].id)
#voices = cls.engine.getProperty('voices')
#cls.engine.setProperty('voice', voices[0].id)
voices = cls.prepareLanguages()
cls.default_voice_id = voices[0].id
cls.engine.setProperty('voice', cls.default_voice_id)
except (RuntimeError, Exception):
print("Sorry, pyttsx3 is not working.")
# goal: if debug mode tell me, else keep quiet
Expand Down Expand Up @@ -79,6 +86,8 @@ def talk(cls, text):
try:
if cls.engine:
#and not SpeakerStatus.debugSwitchOffSpeaker:
if not cls.current_lang in {"German", "system_default"} :
cls.engine.setProperty('voice', cls.default_voice_id)
cls.engine.say(text)
cls.engine.runAndWait()
elif cls.speak:
Expand All @@ -89,6 +98,22 @@ def talk(cls, text):
else:
pass

@classmethod
def talkInLanguage(cls, text, lang="system_default", rate = 200):
if not cls.engine:
if cls.speak:
cls.speak(text)
return
c = cls.voices_dict.get(lang)
if c:
cls.current_lang = lang
cls.engine.setProperty('voice', c)
cls.engine.setProperty('rate', rate)
else:
cls.engine.setProperty('voice', cls.default_voice_id)
cls.engine.say(text)
cls.engine.runAndWait()

@classmethod
def speakFaster(cls):
"""increase words per minute rate"""
Expand All @@ -105,6 +130,24 @@ def speakSlower(cls):
cls.engine.setProperty('rate', rate-50)
print("Geschwindigkeit auf "+str(cls.engine.getProperty('rate')))

@classmethod
def prepareLanguages(cls):
"""Gather voices and languages to be able to switch languages"""
voices = cls.engine.getProperty('voices')
foundvoices = 0
for voice in voices:
if foundvoices == 3:
break
if not "German" in cls.voices_dict and voice.name.__contains__("German"):
cls.voices_dict["German"] = voice.id
foundvoices += 1
if not "French" in cls.voices_dict and voice.name.__contains__("French"):
cls.voices_dict["French"] = voice.id
foundvoices += 1
elif not "English" in cls.voices_dict and voice.name.__contains__("English"):
cls.voices_dict["English"] = voice.id
foundvoices += 1
return voices

# pylint: disable=invalid-name
# pylint: enable=broad-exception-caught
Expand Down

0 comments on commit 9cb6106

Please sign in to comment.