Skip to content

Commit

Permalink
Merge pull request #23 from JeremieGince/simple_tts
Browse files Browse the repository at this point in the history
Simple tts
  • Loading branch information
JeremieGince authored Sep 28, 2023
2 parents da946f6 + cbad073 commit 817db46
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/pythonbasictools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
__url__ = "https://github.com/JeremieGince/PythonBasicTools"
__version__ = "0.0.1-alpha.9"

from .logging import logs_file_setup
from .logging_tools import logs_file_setup
from .device import DeepLib, log_device_setup
from .decorators import log_func
from .multiprocessing import worker_init, multiprocess_logger_init, apply_func_multiprocess
from .multiprocessing_tools import worker_init, multiprocess_logger_init, apply_func_multiprocess
from .slurm import SlurmHostServer, send_slurm_cmd, generate_slurm_cmd
from . import docstring
from . import google_drive
from . import lock
from . import discord
from .simple_tts import say
File renamed without changes.
File renamed without changes.
100 changes: 100 additions & 0 deletions src/pythonbasictools/simple_tts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import os
import time


def say(
text: str,
language: str = 'en-US',
lib: str = "gtts",
cache_file: str = "./.cache/tts.mp3",
delay: float = 0.1,
):
"""
Say the text using the default system voice.
:param text: The text to say.
:type text: str
:param language: The language to use to say the text.
:type language: str
:param lib: The library to use to generate the audio file.
:type lib: str
:param cache_file: The path to the cache file.
:type cache_file: str
:param delay: delay in seconds to play the audio.
:type delay: float
:return: The result of the request.
"""
known_libs = {
"gtts": gen_audio_with_gtts,
"pyttsx3": gen_audio_with_pyttsx3,
}
try:
import playsound
except ImportError:
raise ImportError("The package playsound must be installed. You can install it with `pip install playsound`.")

if lib not in known_libs:
raise ValueError(f"Unknown lib: {lib}. Known libs: {known_libs.keys()}.")

cache_file = known_libs[lib](text, language, cache_file)
time.sleep(delay)
playsound.playsound(os.path.abspath(cache_file))
return cache_file


def gen_audio_with_gtts(
text: str,
language: str = 'en-US',
cache_file: str = "./.cache/gtts/tts.mp3",
):
"""
Generate the audio file using gtts.
:param text: The text to say.
:type text: str
:param language: The language to use to say the text.
:type language: str
:param cache_file: The path to the cache file.
:return: The result of the request.
"""
try:
import gtts
except ImportError:
raise ImportError("The package gtts must be installed. You can install it with `pip install gtts`.")
os.makedirs(os.path.dirname(cache_file), exist_ok=True)
gtts.gTTS(text, lang=language).save(cache_file)
return cache_file


def gen_audio_with_pyttsx3(
text: str,
language: str = 'en-US',
cache_file: str = "./.cache/pyttsx3/tts.mp3",
):
"""
Generate the audio file using pyttsx3.
:param text: The text to say.
:type text: str
:param language: The language to use to say the text.
:type language: str
:param cache_file: The path to the cache file.
:return: The result of the request.
"""
try:
import pyttsx3
except ImportError:
raise ImportError("The package pyttsx3 must be installed. You can install it with `pip install pyttsx3`.")
os.makedirs(os.path.dirname(cache_file), exist_ok=True)
engine = pyttsx3.init()
engine.setVoice(language)
engine.save_to_file(text, cache_file)
engine.runAndWait()
return cache_file


if __name__ == '__main__':
say("Your code has finished.", language="en", lib="gtts")

0 comments on commit 817db46

Please sign in to comment.