-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtts.py
executable file
·69 lines (46 loc) · 1.54 KB
/
tts.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from gtts import gTTS
import pygame
import threading
import os
import time
pygame.init()
MESSAGE_DELAY: float = 1
def _play_stack():
count = 0
while True:
if not message_stack:
continue
path, text = message_stack[-1]
print()
print(f"[now playing: {path}]")
print(f"\"{text}\"")
play_sound_file(path)
count += 1
message_stack.pop()
message_stack = []
sound_thread = threading.Thread(target=_play_stack, daemon=True)
sound_thread.start()
def text_to_file(args: tuple) -> str:
count, text = args
path = f"output{count}.mp3"
try:
file = gTTS(text=text)
file.save(path)
except AssertionError:
print(f"ERROR WITH TTS FOR MESSAGE: [{text}]")
quit()
return path
def play_sound_file(path) -> None:
pygame.mixer.init()
pygame.mixer.music.load(path)
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
time.sleep(.1)
time.sleep(MESSAGE_DELAY)
os.remove(path)
def add_to_stack(ind_text: str):
path = text_to_file(ind_text)
message_stack.insert(0, (path, ind_text[1]))
def add_list_to_stack(ind_list: list):
for ind_sentence in enumerate(ind_list):
add_to_stack(ind_sentence)