-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspeech.py
53 lines (41 loc) · 1.1 KB
/
speech.py
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
import os
import pyttsx3
from gtts import gTTS
from io import BytesIO
import pyglet
temp_mp3_file_name = 'temp.mp3'
def delete_mp3(mp3_file_name: str):
try:
os.remove(mp3_file_name)
except FileNotFoundError:
pass
def get_text_to_speech():
result = input("Write the text to speech (spanish): ")
return result
def init_gtts(text: str):
tts = gTTS(text=text, lang='es')
tts.save(temp_mp3_file_name)
mp3_fp = BytesIO()
tts = gTTS(text=text, lang='es')
tts.write_to_fp(mp3_fp)
return mp3_fp
def play_gtts(mp3_fp: BytesIO):
music = pyglet.media.load(temp_mp3_file_name, file=mp3_fp)
player = pyglet.media.Player()
player.queue(music)
player.play()
def on_eos():
pyglet.app.exit()
#delete_mp3(temp_mp3_file_name)
player.on_eos = on_eos
pyglet.app.run()
def text_to_speech(text: str):
audio = init_gtts(text)
play_gtts(audio)
return "Speech generated successfully"
def main():
text = get_text_to_speech()
audio = init_gtts(text)
play_gtts(audio)
if __name__ == "__main__":
main()