Statistics
| Branch: | Tag: | Revision:

hlrc / tts_bridge / mary / mary_tts_bridge / MaryTTSClient.py @ 295a3f16

History | View | Annotate | Download (3.777 KB)

1 0c15613f Simon Schulz
"""
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 836f314c Robert Haschke
try:
31 909e9261 Robert Haschke
    from http.client import HTTPConnection
32
    from urllib.parse import urlencode
33 836f314c Robert Haschke
except ImportError:  # Python 2
34 909e9261 Robert Haschke
    from httplib import HTTPConnection
35
    from urllib import urlencode
36 0c15613f Simon Schulz
import wave
37
import ctypes
38
import wave
39
import sys
40
41 909e9261 Robert Haschke
42 0c15613f Simon Schulz
class MaryTTSClient:
43 2a6db5bb robocamp
    def __init__(self, voice="bits1-hsmm", locale="de", tts_host="127.0.0.1", tts_port=59125, loglevel=logging.WARNING):
44 909e9261 Robert Haschke
        """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 0c15613f Simon Schulz
52 909e9261 Robert Haschke
        self.logger.info("starting MaryTTSClient (voice="+voice+", locale="+locale+", host="+tts_host+", port="+str(tts_port))
53 0c15613f Simon Schulz
54 909e9261 Robert Haschke
        self.tts_host = tts_host
55
        self.tts_port = tts_port
56
        self.locale   = locale
57
        self.voice    = voice
58 e21d7f2c Simon Schulz
59 0c15613f Simon Schulz
    def __del__(self):
60 909e9261 Robert Haschke
        """destructor
61
        """
62
        self.logger.debug("destructor of MaryTTSClient called")
63 0c15613f Simon Schulz
64
    def config_logger(self, level):
65 909e9261 Robert Haschke
        """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 0c15613f Simon Schulz
75
    def generate_audio(self, message):
76 909e9261 Robert Haschke
        """generate audio from text
77
        :param message: text to synthesize
78
        """
79
        return self.generate(message, "AUDIO")
80 0c15613f Simon Schulz
81
    def generate_phonemes(self, message):
82 909e9261 Robert Haschke
        """generate phoneme list from text
83
        :param message: text to synthesize
84
        """
85
        return self.generate(message, "REALISED_DURATIONS")
86 0c15613f Simon Schulz
87
    def generate(self, message, output_type):
88 909e9261 Robert Haschke
        """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 836f314c Robert Haschke
        conn = HTTPConnection(self.tts_host, self.tts_port)
107 0c15613f Simon Schulz
108 909e9261 Robert Haschke
        conn.request("POST", "/process", params, headers)
109
        response = conn.getresponse()
110 e21d7f2c Simon Schulz
111 909e9261 Robert Haschke
        if response.status != 200:
112
            print(response.getheaders())
113 e21d7f2c Simon Schulz
            conn.close()
114 909e9261 Robert Haschke
            raise RuntimeError("{0}: {1}".format(response.status,response.reason))
115
        return response.read()
116
117 0c15613f Simon Schulz
118 909e9261 Robert Haschke
# test code
119 0c15613f Simon Schulz
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)