""" Created By: ishwor subedi Date: 2024-07-31 """ import base64 from io import BytesIO from gtts import gTTS from gtts.tokenizer import pre_processors class TextToSpeech: def __init__(self): self.preprocessing = [pre_processors.tone_marks, pre_processors.end_of_line, pre_processors.word_sub, pre_processors.abbreviations] def conversion(self, text: str, lang: str, tld: str) -> str: """ Convert text to speech and return the Base64-encoded MP3 data. :param text: The text to convert to speech. :param lang: The language in which to convert the text. :return: Base64-encoded MP3 data as a string. """ tts = gTTS(text=text, lang=lang, slow=False, tld=tld, pre_processor_funcs=self.preprocessing) mp3_fp = BytesIO() tts.write_to_fp(mp3_fp) mp3_fp.seek(0) mp3_binary = mp3_fp.getvalue() base64_mp3 = base64.b64encode(mp3_binary).decode("utf-8") return base64_mp3