Files
api_utils_converse_v2/ai/tts/easy_tts.py
T
khushalps 24f5bc60dc Squashed 'utils_v2/' content from commit aa76ceb
git-subtree-dir: utils_v2
git-subtree-split: aa76ceb7480020f473af5cdb44891d3ebcfc8373
2024-12-05 10:29:29 +05:30

176 lines
6.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
AUTHOR:
Khushal P Soonderji
DATE:
Thursday, 26th Sept., 2024
OBJECTIVE:
To provide a fast way to make audio from TTS engines.
REFERENCES:
01. Usage: https://github.com/myshell-ai/MeloTTS/blob/main/docs/install.md#python-api
02. Installation: https://github.com/myshell-ai/MeloTTS/blob/main/docs/install.md#linux-and-macos-install
DOWNLOADS:
N/A
"""
# *****************************************************************************************************************
# ***** ****
# *** IMPORT ***
# ***** ****
# *****************************************************************************************************************
# To make sibling directories accessible for imports:
import sys
sys.path.append(".")
sys.path.append("..")
# System-level activities:
import io
# To use the AI:
from melo.api import TTS
import numpy as np
import soundfile
# *****************************************************************************************************************
# ***** ****
# *** MACROS / ONE-TIME INIT ***
# ***** ****
# *****************************************************************************************************************
# --- Nothing Yet
# *****************************************************************************************************************
# ***** ****
# *** VARIABLES ***
# ***** ****
# *****************************************************************************************************************
# --- Nothing Yet
# *****************************************************************************************************************
# ***** ****
# *** CLASSES ***
# ***** ****
# *****************************************************************************************************************
class EasyTTS:
__sampling_rate = 44_100
def __init__(
self,
language = "EN",
speed = 1.0
):
self.__audio = np.zeros(1)
self.__language = language
self.__speed = speed
self.__model = TTS(language = language, device = "auto")
self.__speakers = self.__model.hps.data.spk2id
def list_speakers(self):
return list(self.__speakers.keys())
def speak(
self,
text,
speaker
):
this_audio = self.__model.tts_to_file(
text,
self.__speakers[speaker],
speed = self.__speed,
quiet = True
)
self.__audio = np.concatenate((self.__audio, this_audio))
def pause(self, seconds):
self.__audio = np.concatenate((
self.__audio,
np.zeros(int(self.__sampling_rate * seconds))
))
def to_wav(self, path = None):
if path is None:
audio_buffer = io.BytesIO()
soundfile.write(audio_buffer, self.__audio, self.__sampling_rate)
return audio_buffer
else: soundfile.write(path, self.__audio, self.__sampling_rate)
# *****************************************************************************************************************
# ***** ****
# *** MAIN PROGRAM ***
# ***** ****
# *****************************************************************************************************************
if __name__ == "__main__":
speaker = "EN-BR"
tts_maker = EasyTTS(language = "EN", speed = 0.9)
tts_maker.speak(
text = """
Imagine delighting your doctors with a personalized calendar,
crafted from their own cherished memories.
""",
speaker = speaker
)
tts_maker.pause(seconds = 0.3)
tts_maker.speak(
text = """
Every day, as they turn the page,
theyll not only relive those special moments but also remember you,
the one who made it happen.
""",
speaker = speaker
)
tts_maker.pause(seconds = 0.75)
tts_maker.speak(
text = "STEP 1:",
speaker = speaker
)
tts_maker.pause(seconds = 0.3)
tts_maker.speak(
text = "Start by identifying the doctors youd like to engage with, and add them to our app.",
speaker = speaker
)
tts_maker.pause(seconds = 0.3)
tts_maker.speak(
text = "No rush, you can add their photographs later as well.",
speaker = speaker
)
tts_maker.pause(seconds = 0.3)
tts_maker.speak(
text = "With this, your engagement funnel is created.",
speaker = speaker
)
tts_maker.to_wav(r"/home/developer/Downloads/voiceover.wav")