hlrc / tts_bridge / mary / mary_tts_bridge / MaryTTSClient.py @ e34ce888
History | View | Annotate | Download (3.782 KB)
1 |
"""
|
---|---|
2 |
This file is part of hlrc
|
3 |
|
4 |
Copyright(c) sschulz <AT> techfak.uni-bielefeld.de
|
5 |
http://opensource.cit-ec.de/projects/hlrc
|
6 |
|
7 |
This file may be licensed under the terms of the
|
8 |
GNU General Public License Version 3 (the ``GPL''),
|
9 |
or (at your option) any later version.
|
10 |
|
11 |
Software distributed under the License is distributed
|
12 |
on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
|
13 |
express or implied. See the GPL for the specific language
|
14 |
governing rights and limitations.
|
15 |
|
16 |
You should have received a copy of the GPL along with this
|
17 |
program. If not, go to http://www.gnu.org/licenses/gpl.html
|
18 |
or write to the Free Software Foundation, Inc.,
|
19 |
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
20 |
|
21 |
The development of this software was supported by the
|
22 |
Excellence Cluster EXC 277 Cognitive Interaction Technology.
|
23 |
The Excellence Cluster EXC 277 is a grant of the Deutsche
|
24 |
Forschungsgemeinschaft (DFG) in the context of the German
|
25 |
Excellence Initiative.
|
26 |
"""
|
27 |
|
28 |
import logging |
29 |
import sys |
30 |
try:
|
31 |
from http.client import HTTPConnection |
32 |
from urllib.parse import urlencode |
33 |
except ImportError: # Python 2 |
34 |
from httplib import HTTPConnection |
35 |
from urllib import urlencode |
36 |
import wave |
37 |
import ctypes |
38 |
import wave |
39 |
import sys |
40 |
|
41 |
|
42 |
class MaryTTSClient: |
43 |
def __init__(self, voice="cmu-slt-hsmm", locale="en_US", tts_host="127.0.0.1", tts_port=59125, loglevel=logging.WARNING): |
44 |
"""initialise
|
45 |
:param loglevel: optional log level
|
46 |
"""
|
47 |
self.loglevel = loglevel
|
48 |
self.logger = logging.getLogger(__name__)
|
49 |
# create nice and actually usable formatter and add it to the handler
|
50 |
self.config_logger(loglevel)
|
51 |
|
52 |
self.logger.info("starting MaryTTSClient (voice="+voice+", locale="+locale+", host="+tts_host+", port="+str(tts_port)) |
53 |
|
54 |
self.tts_host = tts_host
|
55 |
self.tts_port = tts_port
|
56 |
self.locale = locale
|
57 |
self.voice = voice
|
58 |
|
59 |
def __del__(self): |
60 |
"""destructor
|
61 |
"""
|
62 |
self.logger.debug("destructor of MaryTTSClient called") |
63 |
|
64 |
def config_logger(self, level): |
65 |
"""initialise a nice logger formatting
|
66 |
:param level: log level
|
67 |
"""
|
68 |
formatter = logging.Formatter('%(asctime)s %(name)-30s %(levelname)-8s > %(message)s')
|
69 |
ch = logging.StreamHandler() |
70 |
# ch.setLevel(level)
|
71 |
ch.setFormatter(formatter) |
72 |
self.logger.setLevel(level)
|
73 |
self.logger.addHandler(ch)
|
74 |
|
75 |
def generate_audio(self, message): |
76 |
"""generate audio from text
|
77 |
:param message: text to synthesize
|
78 |
"""
|
79 |
return self.generate(message, "AUDIO") |
80 |
|
81 |
def generate_phonemes(self, message): |
82 |
"""generate phoneme list from text
|
83 |
:param message: text to synthesize
|
84 |
"""
|
85 |
return self.generate(message, "REALISED_DURATIONS") |
86 |
|
87 |
def generate(self, message, output_type): |
88 |
"""generate requested data object from text
|
89 |
:param message: text to synthesize
|
90 |
"""
|
91 |
|
92 |
raw_params = { |
93 |
"INPUT_TEXT": message,
|
94 |
"INPUT_TYPE": "RAWMARYXML", |
95 |
"OUTPUT_TYPE": output_type,
|
96 |
"LOCALE": self.locale, |
97 |
"AUDIO": "WAVE_FILE", |
98 |
"VOICE": self.voice, |
99 |
} |
100 |
|
101 |
params = urlencode(raw_params) |
102 |
headers = {} |
103 |
|
104 |
# conn.set_debuglevel(5)
|
105 |
# open connection to mary server
|
106 |
conn = HTTPConnection(self.tts_host, self.tts_port) |
107 |
|
108 |
conn.request("POST", "/process", params, headers) |
109 |
response = conn.getresponse() |
110 |
|
111 |
if response.status != 200: |
112 |
print(response.getheaders()) |
113 |
conn.close() |
114 |
raise RuntimeError("{0}: {1}".format(response.status,response.reason)) |
115 |
return response.read()
|
116 |
|
117 |
|
118 |
# test code
|
119 |
if __name__ == "__main__": |
120 |
client = MaryTTSClient() |
121 |
audio = client.generate_phonemes("test 1 2 3 4 5 6 7 8 9 10")
|
122 |
print(audio) |