diff --git a/TextToSpeech/tts.py b/TextToSpeech/tts.py new file mode 100644 index 0000000..cb48356 --- /dev/null +++ b/TextToSpeech/tts.py @@ -0,0 +1,60 @@ +# possible replacement pyttsx3 +from gtts import gTTS +import os +from utils import * +# tts audio config +PATH_TO_AUDIO_DIR = r"audio/" +DEFAULT_AUDIO_FILE = PATH_TO_AUDIO_DIR + "temporary.mp3" + + +class Speaker: + def __init__(self, language="en-us"): + self._language = language + self._tts = gTTS(lang=self._language, text="dummy") + + # public methods + def set_language(self, language): + """ + Sets operating speaking _language. + :param str language: _language code + :rtype: None + :return: void method + """ + assert (isinstance(language, str)) + self._language = language + self._tts.lang = self._language + + def get_language(self): + """ + Returns speaking _language. + :rtype:str + :return: speaking _language + """ + return self._language + + def save_speech_and_play(self, text=""): + """ + Speak out the given text. Text must not be empty string. + :param str text: text to be spoken + :rtype: None + :return: void method + """ + assert (isinstance(text, str)) + if text != '': + self._speak(text, str(get_current_timestamp()) + ".mp3") + + # private methods + def _speak(self, text, file_name=DEFAULT_AUDIO_FILE): + """ + Speak out and play audio. + :param str text: + :param str file_name: audio file in which speech will be saved + :rtype: None + :return:void method + """ + assert (isinstance(text, str)) + if file_name != DEFAULT_AUDIO_FILE: + file_name = PATH_TO_AUDIO_DIR + file_name + self._tts.text = text + self._tts.save(file_name) + play_audio(file_name) diff --git a/TextToSpeech/tts_test.py b/TextToSpeech/tts_test.py new file mode 100644 index 0000000..0ef62c3 --- /dev/null +++ b/TextToSpeech/tts_test.py @@ -0,0 +1,5 @@ +from tts import Speaker + +speaker = Speaker("en-us") +print(speaker._speak("Marry had a little lamb!")) +print(speaker.save_speech_and_play(text="Hello, world!")) diff --git a/TextToSpeech/utils.py b/TextToSpeech/utils.py new file mode 100644 index 0000000..de57c2b --- /dev/null +++ b/TextToSpeech/utils.py @@ -0,0 +1,21 @@ +import time +from playsound import playsound + +def play_audio(file_name): + """ + Play audio file. + :param str file_name: name of file that will be played. + :rtype: None + :return: void method + """ + assert (isinstance(file_name, str)) + playsound(file_name) + + +def get_current_timestamp(): + """ + Returns current timestamp as str. + :rtype: str + :return: current timestamp (Return the current time in seconds since the Epoch) + """ + return time.time()