Statistics
| Branch: | Tag: | Revision:

hlrc / tts_bridge / mary / mary_tts_bridge / MaryTTSClient.py @ 5eb9025f

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