1
2
3 """HTTP wrapper around Emacspeak speech server.
4
5 Speech server is launched on HTTP server startup.
6
7 Speech commands are invoked via URLs:
8
9 http://host:port/cmd?arg
10
11 calls speaker.cmd(arg)
12
13 """
14
15 __id__ = "$Id: HTTPSpeaker.py 7067 2011-06-25 02:49:51Z tv.raman.tv $"
16 __author__ = "$Author: tv.raman.tv $"
17 __version__ = "$Revision: 7067 $"
18 __date__ = "$Date: 2011-06-24 19:49:51 -0700 (Fri, 24 Jun 2011) $"
19 __copyright__ = "Copyright (c) 2005 T. V. Raman"
20 __license__ = "LGPL"
21
22
23 from speaker import Speaker
24 from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
25 import sys
26 import os
27 import urllib
28
30
31 """Speech server via HTTP."""
32
33 - def __init__(self, address, handler,
34 engine='outloud',
35 rate = 75):
36 """Initialize HTTP listener."""
37 HTTPServer.__init__(self, address, handler)
38 self.speaker = Speaker(engine,
39 'localhost',
40 {'punctuations' : 'some',
41 'rate' : rate})
42
44
45 """Handle HTTP Speak requests."""
46 handlers = ['say',
47 'speak',
48 'letter',
49 'addText',
50 'silence',
51 'tone',
52 'stop',
53 'punctuation',
54 'rate',
55 'allcaps',
56 'capitalize',
57 'splitcaps',
58 'reset',
59 'shutdown',
60 'version' ]
61
63 """Not Implemented."""
64
65
66 if hasattr(self.server.speaker, cmd):
67 method = getattr(self.server.speaker, cmd)
68 if arg is None:
69 method()
70 else:
71 method(urllib.unquote(arg))
72 self.send_response(200, self.path)
73 else: self.send_error(501, "Speaker error")
74
76 """Handle speech request in a POST message. """
77 contentLength = self.headers.getheader('content-length')
78 if contentLength:
79 contentLength = int(contentLength)
80 inputBody = self.rfile.read(contentLength)
81 sys.stderr.write(inputBody + "\n")
82 if inputBody.startswith("speak:"):
83 text = inputBody[6:]
84 self.server.speaker.sayUtterances([text] )
85 self.send_response(200, 'OK')
86 elif inputBody == "stop":
87 self.server.speaker.stop()
88 self.send_response(200, 'OK')
89 elif inputBody == "isSpeaking":
90 self.send_response(200, 'OK')
91 self.send_header("Content-type", "text/html")
92 self.end_headers()
93 self.wfile.write("0")
94 else:
95 self.send_error(501, 'Unknown POST message ' + inputBody)
96
98 if sys.argv[1:]:
99 engine = sys.argv[1]
100 else:
101 engine=os.getenv('DTK_PROGRAM','outloud')
102 if sys.argv[2:]:
103 port = int(sys.argv[2])
104 else:
105 port = 8000
106 if sys.argv[3:]:
107 rate = int(sys.argv[3])
108 else:
109 rate = 75
110 server_address = ('', port)
111 httpd = HTTPSpeaker (server_address,
112 SpeakHTTPRequestHandler, engine, rate)
113 httpd.serve_forever()
114
115
116 if __name__ == '__main__':
117 start()
118