Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simple tts #24

Merged
merged 2 commits into from
Sep 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/pythonbasictools/simple_tts.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ def say(
lib: str = "gtts",
cache_file: str = "./.cache/tts.mp3",
delay: float = 0.1,
rm_cache_file: bool = True,
raise_error: bool = False,
):
"""
Say the text using the default system voice.
Expand All @@ -22,6 +24,8 @@ def say(
:type cache_file: str
:param delay: delay in seconds to play the audio.
:type delay: float
:param rm_cache_file: Whether to remove the cache file after playing it.
:type rm_cache_file: bool

:return: The result of the request.
"""
Expand All @@ -39,7 +43,17 @@ def say(

cache_file = known_libs[lib](text, language, cache_file)
time.sleep(delay)
playsound.playsound(os.path.abspath(cache_file))
try:
playsound.playsound(os.path.abspath(cache_file))
except Exception as e:
if raise_error:
raise e
else:
print(e)
finally:
if rm_cache_file:
if os.path.exists(cache_file):
os.remove(cache_file)
return cache_file


Expand Down