-
Notifications
You must be signed in to change notification settings - Fork 0
/
tts.py
65 lines (59 loc) · 2.63 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
import traceback
import sys
import tornado.process
import tornado.ioloop
from tornado.gen import Task, Return, coroutine
from parametros import expuesto
import os.path
STREAM = tornado.process.Subprocess.STREAM
class TTS(object):
@expuesto
def PORCENTAJE_VOLUMEN():
""" Porcentaje de volumen al que se traduce el texto. """
return 75
def tts(self, texto, callback_reproducir, callback_fin=None):
# Para poder ejecutar el proceso y en paralelo seguir procesando el ioloop,
# creo una coroutine y la lanzo como un callback del ioloop.
tornado.ioloop.IOLoop.current().add_callback(self.tts_cor, texto, callback_reproducir, callback_fin)
@coroutine
def tts_cor(self, texto, callback_reproducir, callback_fin):
print("TTS.tts_cor("+str(texto)+","+str(callback_reproducir)+","+str(callback_fin)+")")
fn="/tmp/"+str(hash(texto))
print("TTS.tts_cor: fn="+str(fn))
print("TTS.tts_cor: isfile="+str(os.path.isfile(fn+".44100.wav")))
# /tmp nos hace de cache simple. Si hubiera mucha variabilidad, eso no bastaria.
if not os.path.isfile(fn+".44100.wav"):
try:
print("TTS.tts_cor: entrando")
cmd = ['/usr/bin/pico2wave','-w',fn+".wav",'-l','es-ES',"<volume level='"+str(int(TTS.PORCENTAJE_VOLUMEN))+"'>"+texto]
print("TTS.tts_cor: cmd="+str(cmd))
cs = self.call_subprocess(cmd)
print("TTS.tts_cor: cs="+str(cs))
result, error = yield cs
print("TTS.tts_cor: result="+str(result))
print("TTS.tts_cor: error="+str(error))
cmd = ['/usr/bin/sox',"-G",fn+".wav",fn+".44100.wav","channels","1","rate","44100"]
cs = self.call_subprocess(cmd)
print("TTS.tts_cor: cs="+str(cs))
result, error = yield cs
print("TTS.tts_cor: result="+str(result))
print("TTS.tts_cor: error="+str(error))
except:
print(sys.exc_info())
traceback.print_exc()
raise
print("TTS.tts_cor: callback_reproducir")
callback_reproducir(fn+".44100.wav", callback=callback_fin)
@coroutine
def call_subprocess(self, args):
print("TTS.call_subprocess "+str(args))
sub_process = tornado.process.Subprocess(
args, stdin=STREAM, stdout=STREAM, stderr=STREAM
)
print("TTS.call_subprocess "+str(sub_process))
#yield Task(sub_process.stdin.write, "")
result, error = yield [
Task(sub_process.stdout.read_until_close),
Task(sub_process.stderr.read_until_close)
]
raise Return((result, error))