-
Notifications
You must be signed in to change notification settings - Fork 1
/
tts.py
71 lines (61 loc) · 1.94 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
70
71
from gtts import gTTS
from playsound import playsound
import os
import subprocess
import json
import secrets
active_process = None
# Prereq:
# Install sox (Linux)
# Arch Linux:
# $ sudo pacman -S sox
# Ubuntu:
# $ sudo apt-get update
# $ sudo apt-get install sox
# $ sudo apt-get install libsox-fmt-all
def play_mp3(path, active_process):
try:
active_process.terminate()
except:
pass
DEVNULL = open(os.devnull, 'wb')
try:
active_process = subprocess.Popen(['play', path, 'tempo', '1.5'], stdout = DEVNULL, stderr = DEVNULL)
finally:
DEVNULL.close()
f = open('db.json',)
data = json.load(f)
flow = [
["_greeting", "_education", "_finances", "_exit"],
["_greeting", "_finances", "_exit"],
["_greeting", "_aboutNEU", "_finances","_exit"],
["_greeting", "_finances", "_grill", "_exit"],
["_greeting", "_education", "_finances", "_grill", "_exit"],
["_greeting", "_aboutNEU", "_education", "_exit"],
["_greeting", "_aboutNEU", "_education", "_finances", "_exit"],
]
while (True):
print("----------START----------")
chosen_flow = secrets.choice(flow)
for item in chosen_flow:
state = data[item]
while(state):
keys = list(state.keys())
chosen_key = secrets.choice(keys)
print("VO: "+chosen_key, end="")
speech = gTTS(text = chosen_key, lang='en', slow=False)
speech.save('temp.mp3')
#####
# If sox isn't installed use the playsound library
play_mp3('temp.mp3', active_process)
# os.system("play temp.mp3 tempo 1.5")
# playsound('temp.mp3')
#####
try:
input("")
except EOFError:
state = state[chosen_key]
continue
state = state[chosen_key]
print("-----------END-----------", end="")
input("")