#!/bin/env python import winsound import httplib, urllib # A very basic Python class for accessing # the MARY TTS system using the modern # HTTP server. # Warning, this is probably ghastly Python, # most of my time of late has been with # other languages, so I'm not up to date # with all the stylistic conventions of # modern Python. # This does seem to work OK though. class winmaryclient: """A basic handler for MARY-TTS HTTP clients At present, there is no checking for allowed voices, locales, and so on. Most of the useful parameters can be accessed by get_ and set_ methods. Relying on winsound, this is Windows specific. """ def __init__(self): self.host = "127.0.0.1" self.port = 59125 self.input_type = "TEXT" self.output_type = "AUDIO" self.audio = "WAVE_FILE" self.locale = "en_GB" self.voice = "dfki-prudence-hsmm" def set_host(self, a_host): self.host = a_host def get_host(self): self.host def set_port(self, a_port): self.port = a_port def get_port(self): self.port def set_input_type(self, type): self.input_type = type def get_input_type(self): self.input_type def set_output_type(self, type): self.output_type = type def get_output_type(self): self.output_type def set_locale(self, a_locale): self.locale = a_locale def get_locale(self): self.locale def set_audio(self, audio_type): self.audio = audio_type def get_audio(self): self.audio def set_voice(self, a_voice): self.voice = a_voice def get_voice(self): self.voice def say(self, message): """Given a message in message, say it.""" raw_params = {"INPUT_TEXT": message, "INPUT_TYPE": self.input_type, "OUTPUT_TYPE": self.output_type, "LOCALE": self.locale, "AUDIO": self.audio, "VOICE": self.voice, } params = urllib.urlencode(raw_params) headers = {} # Open connection to self.host, self.port. conn = httplib.HTTPConnection(self.host, self.port) # conn.set_debuglevel(5) conn.request("POST", "/process", params, headers) response = conn.getresponse() if response.status != 200: print response.getheaders() raise RuntimeError("{0}: {1}".format(response.status, response.reason)) content_type = response.getheader("Content-Type") if content_type.find("audio") == 0: sound = response.read() winsound.PlaySound(sound, winsound.SND_MEMORY) else: return response.read # If this is invoked as a program, just give # a greeting to show it is working. if __name__ == "__main__": client = winmaryclient() client.say("hello from Mary Text to Speech, with Python.") # vi:set sw=4 et: