Revision f27f1860 tts_bridge/mary/mary_tts_bridge/MaryTTSClient.py

View differences:

tts_bridge/mary/mary_tts_bridge/MaryTTSClient.py
26 26
"""
27 27

  
28 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 29
import sys
39 30
try:
40
	from http.client import HTTPConnection
41
	from urllib.parse import urlencode
31
    from http.client import HTTPConnection
32
    from urllib.parse import urlencode
42 33
except ImportError:  # Python 2
43
	from httplib import HTTPConnection
44
	from urllib import urlencode
34
    from httplib import HTTPConnection
35
    from urllib import urlencode
45 36
import wave
46 37
import ctypes
47 38
import wave
48 39
import sys
49 40

  
41

  
50 42
class MaryTTSClient:
51 43
    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)
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)
59 51

  
60
	self.logger.info("starting MaryTTSClient (voice="+voice+", locale="+locale+", host="+tts_host+", port="+str(tts_port))
52
        self.logger.info("starting MaryTTSClient (voice="+voice+", locale="+locale+", host="+tts_host+", port="+str(tts_port))
61 53

  
62
	self.tts_host = tts_host
63
	self.tts_port = tts_port
64
	self.locale   = locale
65
	self.voice    = voice
54
        self.tts_host = tts_host
55
        self.tts_port = tts_port
56
        self.locale   = locale
57
        self.voice    = voice
66 58

  
67 59
    def __del__(self):
68
	"""destructor
69
	"""
70
	self.logger.debug("destructor of MaryTTSClient called")
60
        """destructor
61
        """
62
        self.logger.debug("destructor of MaryTTSClient called")
71 63

  
72 64
    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)
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)
82 74

  
83 75
    def generate_audio(self, message):
84
	"""generate audio from text
85
	:param message: text to synthesize
86
	"""
87
	return self.generate(message, "AUDIO")
76
        """generate audio from text
77
        :param message: text to synthesize
78
        """
79
        return self.generate(message, "AUDIO")
88 80

  
89 81
    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")
82
        """generate phoneme list from text
83
        :param message: text to synthesize
84
        """
85
        return self.generate(message, "REALISED_DURATIONS")
94 86

  
95 87
    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
	params = urlencode(raw_params)
110
	headers = {}
111

  
112
	#conn.set_debuglevel(5)
113
	#open connection to mary server
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
114 106
        conn = HTTPConnection(self.tts_host, self.tts_port)
115 107

  
116
	conn.request("POST", "/process", params, headers)
117
	response = conn.getresponse()
108
        conn.request("POST", "/process", params, headers)
109
        response = conn.getresponse()
118 110

  
119
	if response.status != 200:
120
	    print(response.getheaders())
111
        if response.status != 200:
112
            print(response.getheaders())
121 113
            conn.close()
122
	    raise RuntimeError("{0}: {1}".format(response.status,response.reason))
123
	return response.read()
114
            raise RuntimeError("{0}: {1}".format(response.status,response.reason))
115
        return response.read()
116

  
124 117

  
125
#test code
118
# test code
126 119
if __name__ == "__main__":
127 120
    client = MaryTTSClient()
128 121
    audio = client.generate_phonemes("test 1 2 3 4 5 6 7 8 9 10")

Also available in: Unified diff