CineAI commited on
Commit
5d817ad
1 Parent(s): d834bcd

Update audio_processing/T2A.py

Browse files
Files changed (1) hide show
  1. audio_processing/T2A.py +33 -34
audio_processing/T2A.py CHANGED
@@ -1,56 +1,55 @@
1
- import logging
2
-
3
- from io import BytesIO
4
  from typing import Optional
5
 
6
- import librosa
7
- import soundfile as sf
8
  from streamlit_TTS import auto_play, text_to_audio
9
 
10
  from .config import pipe_tts
11
 
12
- SAMPLING_RATE = 16_000
13
 
14
 
15
  class T2A:
16
- def __init__(self, input_text: Optional[str] = None):
17
- self.text = input_text
18
- self.output_model = pipe_tts(input_text)
19
-
20
- def __get_duration(self, raw: bytes) -> float:
21
- chunk = BytesIO(raw)
22
- audio, sample_rate = librosa.load(chunk, sr=SAMPLING_RATE)
23
- duration = librosa.get_duration(y=audio, sr=sample_rate)
24
- return duration
25
-
26
- def autoplay(self, lang: str = "en") -> None:
27
- if self.text is not None:
 
28
  if isinstance(self.text, str):
29
- audio = text_to_audio(self.text, language=lang)
30
  auto_play(audio)
31
- else: # more checking
32
  text = f"Text you provide is {type(self.text)} accepted only string type"
33
  audio = text_to_audio(text, language=lang)
34
  auto_play(audio)
35
  else:
36
- raise Exception("Text is None")
37
 
38
- def get_audio(self) -> tuple[bytes, int, float]:
39
- try:
40
- synth = self.output_model["audio"][0]
41
 
42
- print(f"synth : {synth}")
43
 
44
- with BytesIO() as buffer:
45
- sf.write(buffer, synth, SAMPLING_RATE, format='wav')
46
- output = buffer.getvalue() # bytes
47
 
48
- print(f"type : {type(output)}")
49
 
50
- duration = self.__get_duration(output)
51
 
52
- print(f"duration : {duration}")
53
 
54
- return output, SAMPLING_RATE, duration
55
- except Exception as e:
56
- logging.error(e)
 
1
+ # from io import BytesIO
 
 
2
  from typing import Optional
3
 
4
+ # import librosa
5
+ # import soundfile as sf
6
  from streamlit_TTS import auto_play, text_to_audio
7
 
8
  from .config import pipe_tts
9
 
10
+ # SAMPLING_RATE = 16_000
11
 
12
 
13
  class T2A:
14
+
15
+ # def __get_duration(self, raw: bytes) -> float:
16
+ # chunk = BytesIO(raw)
17
+ # audio, sample_rate = librosa.load(chunk, sr=SAMPLING_RATE)
18
+ # duration = librosa.get_duration(y=audio, sr=sample_rate)
19
+ # return duration
20
+
21
+ def autoplay(self, input_text: Optional[str] = None, lang: str = "en") -> None:
22
+
23
+ text = input_text
24
+ output_model = pipe_tts(input_text)
25
+
26
+ if text is not None:
27
  if isinstance(self.text, str):
28
+ audio = text_to_audio(text, language=lang)
29
  auto_play(audio)
30
+ else:
31
  text = f"Text you provide is {type(self.text)} accepted only string type"
32
  audio = text_to_audio(text, language=lang)
33
  auto_play(audio)
34
  else:
35
+ auto_play("Please check the input text you have provided, it has a value of None")
36
 
37
+ # def get_audio(self) -> tuple[bytes, int, float]:
38
+ # try:
39
+ # synth = self.output_model["audio"][0]
40
 
41
+ # print(f"synth : {synth}")
42
 
43
+ # with BytesIO() as buffer:
44
+ # sf.write(buffer, synth, SAMPLING_RATE, format='wav')
45
+ # output = buffer.getvalue() # bytes
46
 
47
+ # print(f"type : {type(output)}")
48
 
49
+ # duration = self.__get_duration(output)
50
 
51
+ # print(f"duration : {duration}")
52
 
53
+ # return output, SAMPLING_RATE, duration
54
+ # except Exception as e:
55
+ # logging.error(e)