# from io import BytesIO from typing import Optional # import librosa # import soundfile as sf from streamlit_TTS import auto_play, text_to_audio from .config import pipe_tts # SAMPLING_RATE = 16_000 class T2A: # def __get_duration(self, raw: bytes) -> float: # chunk = BytesIO(raw) # audio, sample_rate = librosa.load(chunk, sr=SAMPLING_RATE) # duration = librosa.get_duration(y=audio, sr=sample_rate) # return duration def autoplay(self, input_text: Optional[str] = None, lang: str = "en") -> None: text = input_text output_model = pipe_tts(input_text) if text is not None: if isinstance(self.text, str): audio = text_to_audio(text, language=lang) auto_play(audio) else: text = f"Text you provide is {type(self.text)} accepted only string type" audio = text_to_audio(text, language=lang) auto_play(audio) else: auto_play("Please check the input text you have provided, it has a value of None") # def get_audio(self) -> tuple[bytes, int, float]: # try: # synth = self.output_model["audio"][0] # print(f"synth : {synth}") # with BytesIO() as buffer: # sf.write(buffer, synth, SAMPLING_RATE, format='wav') # output = buffer.getvalue() # bytes # print(f"type : {type(output)}") # duration = self.__get_duration(output) # print(f"duration : {duration}") # return output, SAMPLING_RATE, duration # except Exception as e: # logging.error(e)