-
Notifications
You must be signed in to change notification settings - Fork 0
/
TextToSpeech.py
147 lines (125 loc) · 4.72 KB
/
TextToSpeech.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
# -*- coding: utf-8 -*-
#-----------------------------------------------------------------------------
#
# TextToSpeech functions
#
# Copyright (C) 2021 Florian Pose
#
# This file is part of Alarm Display.
#
# Alarm Display is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Alarm Display is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along with
# Alarm Display. If not, see <http://www.gnu.org/licenses/>.
#
#-----------------------------------------------------------------------------
import subprocess
import tempfile
import os
from gtts import gTTS
from PyQt5.QtCore import *
#-----------------------------------------------------------------------------
class TextToSpeech(QObject):
def __init__(self, config, logger):
super(TextToSpeech, self).__init__()
self.config = config
self.logger = logger
self.ttsDelay = self.config.getint("sound", "tts_delay",
fallback = 30)
self.repetitions = self.config.getint("sound", "tts_repetitions",
fallback = 10)
self.padFile = self.config.get("sound", "tts_pad_file",
fallback = '')
self.timer = QTimer(self)
self.timer.setInterval(self.ttsDelay * 1000)
self.timer.setSingleShot(True)
self.timer.timeout.connect(self.play)
self.thread = QThread()
self.player = None
self.file = None
self.repetition = 0
def clear(self):
self.logger.info('Clearing TTS.')
self.file = None
def setText(self, text):
self.logger.info('Generating TTS for %s', text)
try:
new_file = tempfile.NamedTemporaryFile(suffix = '.mp3')
except:
self.logger.error('Failed to generate temporary file for TTS.')
return
try:
tts = gTTS(text = text, lang = 'de')
tts.save(new_file.name)
except:
self.logger.error('TTS failed.')
return
self.file = new_file
if self.padFile:
try:
pad_file = tempfile.NamedTemporaryFile(suffix = '.wav')
except:
self.logger.error( \
'Failed to generate temporary file for padding.')
return
cmd = 'sox ' + self.padFile + ' ' + new_file.name + ' ' \
+ self.padFile + ' ' + pad_file.name
self.logger.info('Executing %s', cmd)
try:
ret = os.system(cmd)
except Exception as e:
self.logger.error('Padding failed: %s', e)
return
if ret == 0:
self.file = pad_file
def start(self):
self.repetition = 0
if self.repetition < self.repetitions:
self.timer.start()
def play(self):
if self.player:
self.logger.info('TTS player still active.')
return
if self.file:
self.logger.info('Starting TTS player...')
self.player = Player(self.logger, self.file.name)
self.player.finished.connect(self.playerFinished)
self.player.finished.connect(self.thread.quit)
self.player.moveToThread(self.thread)
self.thread.started.connect(self.player.play)
self.thread.start()
self.repetition += 1
if self.repetition < self.repetitions:
self.timer.start()
def playerFinished(self):
self.logger.info('TTS player finished.')
self.player = None
#-----------------------------------------------------------------------------
class Player(QObject):
finished = pyqtSignal()
def __init__(self, logger, soundFile):
super(Player, self).__init__()
self.logger = logger
self.cmd = ['play', '-q', soundFile]
def __del__(self):
self.logger.info(u'Deleting TTS player')
@pyqtSlot()
def play(self):
self.logger.info(u'Running %s', self.cmd)
try:
play = subprocess.Popen(self.cmd, stdout = subprocess.DEVNULL)
play.wait()
if play.returncode != 0:
self.logger.error(u'TTS play command failed.')
except:
self.logger.error('Failed to execute player.', exc_info = True)
self.finished.emit()
#-----------------------------------------------------------------------------