forked from nerdaxic/glados-voice-assistant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgladosTTS.py
154 lines (114 loc) · 3.76 KB
/
gladosTTS.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import os
import wave
from random import randint
import _thread as thread
from threading import Timer
from gladosSerial import *
from gladosServo import *
from glados_functions import *
import sys
import urllib.parse
import re
import json
import random
import shutil
from subprocess import call
import glados_settings
glados_settings.load_from_file()
if(glados_settings.settings["tts"]["api"] == ''): from glados_tts.engine import *
from glados_functions import *
synthFolder = glados_settings.settings["tts"]["cache_folder"] + "/"
def playFile(filename):
call(["aplay", "-q", filename])
# Turns units etc into speakable text
def cleanTTSLine(line):
line = line.replace("sauna", "incinerator")
line = line.replace("'", "")
line = line.lower()
if re.search("-\d", line):
line = line.replace("-", "negative ")
return line
# Cleans filename for the sample
def cleanTTSFile(line):
filename = "GLaDOS-tts-"+cleanTTSLine(line).replace(" ", "-")
filename = filename.replace("!", "")
filename = filename.replace("°c", "degrees celcius")
filename = filename.replace(",", "")+".wav"
return filename
# Return the path of a TTS sample if found in the library
def checkTTSLib(line):
line = cleanTTSLine(line)
filename = cleanTTSFile(line)
if os.path.isfile(synthFolder+filename):
return synthFolder+filename
else:
return False
# Get GLaDOS TTS Sample
def fetchTTSSample(line):
# Local TTS Engine
if(glados_settings.settings["tts"]["api"] == ''):
if(glados_tts(cleanTTSLine(line)) == True):
print('Success: TTS sample "'+line+'" fetched')
setEyeAnimation("idle")
return True
# Remote TTS Engine API
else:
text = urllib.parse.quote(cleanTTSLine(line))
TTSCommand = 'curl -L --retry 5 --get --fail -o audio/GLaDOS-tts-temp-output.wav '+glados_settings.settings["tts"]["api"]+text
setEyeAnimation("wait")
TTSResponse = os.system(TTSCommand)
if(TTSResponse == 0):
print('Success: TTS sample "'+line+'" fetched')
setEyeAnimation("idle")
return True
# Complain about speech synthesis core
setEyeAnimation("angry")
playFile(os.path.dirname(os.path.abspath(__file__))+"/audio/GLaDOS-tts-error.wav")
return False
## Speak out the line
def speak(line, cache=False):
started_speaking()
line = cleanTTSLine(line)
# Generate filename
file = checkTTSLib(line)
# Check if file exists
if file:
# Animate eye
if eye_position_script(line) == False:
eye_position_random()
print("\033[1;33mGLaDOS:\033[0;37m " + line.capitalize())
# Speak from cache
playFile(file)
# TTS Sample not in cache...
else:
print("\033[1;94mINFO:\033[;97m The audio sample does not exist, generating...")
setEyeAnimation("wait")
# Generate line and save to TTS-folder
if(fetchTTSSample(line)):
setEyeAnimation("idle")
if eye_position_script(line) == False:
eye_position_random()
print("\033[1;33mGLaDOS:\033[0;37m " + line.capitalize())
# Speak
playFile("./audio/GLaDOS-tts-temp-output.wav")
if(cache):
shutil.copyfile("audio/GLaDOS-tts-temp-output.wav", synthFolder+cleanTTSFile(line))
stopped_speaking()
eye_position_default()
# Load greetings from a json file into a "playlist"
file = open("skills/glados_greetings.json")
greetings = json.load(file)
# Shuffle the list
random.shuffle(greetings)
# Global variable to make sure same greeting does not come up twice in a row
greeting_index = 0;
# Fetch the next greeting from the playlist
def fetch_greeting():
global greeting_index
greeting = greetings[greeting_index]["greeting"]
greeting_index += 1;
# Loop the "playlist"
if(greeting_index >= len(greetings)):
greeting_index = 0
random.shuffle(greetings)
return greeting