File size: 1,668 Bytes
9b0d264
 
d5436e0
 
 
3a802c4
4bb9300
d5436e0
9b0d264
 
4bb9300
d5436e0
d2150bd
4bb9300
 
d5436e0
be26bab
0fb503b
4bb9300
d5436e0
4ac82ef
6298db6
 
 
 
be26bab
 
 
 
d2150bd
d5436e0
be26bab
d5436e0
d2150bd
 
 
 
d5436e0
9b0d264
0fb503b
06fe464
0fb503b
06fe464
4bb9300
0fb503b
d5436e0
4bb9300
6298db6
 
b3fbe5e
6298db6
 
06fe464
6298db6
9b0d264
d5436e0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import logging

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 __init__(self, input_text: Optional[str] = None):
        self.text = input_text
        self.output_model = pipe_tts(input_text)

    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, lang: str = "en") -> None:
        if self.text is not None:
            if isinstance(self.text, str):
                audio = text_to_audio(self.text, language=lang)
                auto_play(audio)
            else: # more checking
                text = f"Text you provide is {type(self.text)} accepted only string type"
                audio = text_to_audio(text, language=lang)
                auto_play(audio)
        else:
            raise Exception("Text is 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)