Statistics
| Branch: | Tag: | Revision:

hlrc / tts_bridge / mary / mary_tts_bridge / MaryTTSClient.py @ 836f314c

History | View | Annotate | Download (3.549 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
#try:
30
#        import rsb
31
#except ImportError:
32
#        RSB_SUPPORT = False
33
#else:
34
#        from MiddlewareRSB import *
35
#        RSB_SUPPORT = True
36
37
#from MiddlewareROS import *
38
import sys
39 836f314c Robert Haschke
try:
40
        from http.client import HTTPConnection
41
        from urllib.parse import urlencode
42
except ImportError:  # Python 2
43
        from httplib import HTTPConnection
44
        from urllib import urlencode
45 0c15613f Simon Schulz
import wave
46
import ctypes
47
import wave
48
import sys
49
50
class MaryTTSClient:
51
    def __init__(self, voice="cmu-slt-hsmm", locale="en_US", tts_host="127.0.0.1", tts_port=59125, loglevel=logging.WARNING):
52
        """initialise
53
        :param  loglevel: optional log level
54
        """
55
        self.loglevel = loglevel
56
        self.logger = logging.getLogger(__name__)
57
        # create nice and actually usable formatter and add it to the handler
58
        self.config_logger(loglevel)
59
60
        self.logger.info("starting MaryTTSClient (voice="+voice+", locale="+locale+", host="+tts_host+", port="+str(tts_port))
61
62
        self.tts_host = tts_host
63
        self.tts_port = tts_port
64
        self.locale   = locale
65
        self.voice    = voice
66 e21d7f2c Simon Schulz
67 0c15613f Simon Schulz
    def __del__(self):
68
        """destructor
69
        """
70
        self.logger.debug("destructor of MaryTTSClient called")
71
72
    def config_logger(self, level):
73
        """initialise a nice logger formatting
74
        :param  level: log level
75
        """
76
        formatter = logging.Formatter('%(asctime)s %(name)-30s %(levelname)-8s > %(message)s')
77
        ch = logging.StreamHandler()
78
        #ch.setLevel(level)
79
        ch.setFormatter(formatter)
80
        self.logger.setLevel(level)
81
        self.logger.addHandler(ch)
82
83
    def generate_audio(self, message):
84
        """generate audio from text
85
        :param message: text to synthesize
86
        """
87
        return self.generate(message, "AUDIO")
88
89
    def generate_phonemes(self, message):
90
        """generate phoneme list from text
91
        :param message: text to synthesize
92
        """
93
        return self.generate(message, "REALISED_DURATIONS")
94
95
    def generate(self, message, output_type):
96
        """generate requested data object from text
97
        :param message: text to synthesize
98
        """
99
100
        raw_params = {
101
            "INPUT_TEXT": message,
102
            "INPUT_TYPE": "RAWMARYXML",
103
            "OUTPUT_TYPE": output_type,
104
            "LOCALE": self.locale,
105
            "AUDIO": "WAVE_FILE",
106
            "VOICE": self.voice,
107
        }
108
109 836f314c Robert Haschke
        params = urlencode(raw_params)
110 0c15613f Simon Schulz
        headers = {}
111
112
        #conn.set_debuglevel(5)
113 e21d7f2c Simon Schulz
        #open connection to mary server
114 836f314c Robert Haschke
        conn = HTTPConnection(self.tts_host, self.tts_port)
115 0c15613f Simon Schulz
116
        conn.request("POST", "/process", params, headers)
117
        response = conn.getresponse()
118 e21d7f2c Simon Schulz
119 0c15613f Simon Schulz
        if response.status != 200:
120 836f314c Robert Haschke
            print(response.getheaders())
121 e21d7f2c Simon Schulz
            conn.close()
122 0c15613f Simon Schulz
            raise RuntimeError("{0}: {1}".format(response.status,response.reason))
123
        return response.read()
124
125
#test code
126
if __name__ == "__main__":
127
    client = MaryTTSClient()
128
    audio = client.generate_phonemes("test 1 2 3 4 5 6 7 8 9 10")
129
    print(audio)