/
ttstest.py
128 lines (102 loc) · 3.59 KB
/
ttstest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!usr/bin/env python
#encoding=utf-8
"""
#--------------------------------------------------------------------------
File: ttstest.py
Description:
The sample code for ITRI text to speech web service. Beofre testing it,
it is needed to apply a service account
http://tts.itri.org.tw/member/registeration.php
In shell, run the following script:
$ python ttstest.py -a account -p password -t message
If success, the translated voice will be store in "message" with .wav file extension.
Author: Edward Yang
#--------------------------------------------------------------------------
"""
import urllib2
from time import sleep
from SOAPpy import SOAPProxy
WSDLFile = "http://tts.itri.org.tw/TTSService/Soap_1_3.php?wsdl"
url = "http://tts.itri.org.tw/TTSService/Soap_1_3.php"
namespace = "http://tts.itri.org.tw/TTSService"
def textToSpeech(name, passwd, xlatText):
proxy = WSDL.Proxy(WSDLFile)
proxy.soapproxy.config.debug = 1
# NOTE. The following line is workaround for SOAPpy namespace issue
proxy.methods['ConvertSimple'].namespace = proxy.wsdl.targetNamespace
#print proxy.methods.keys()
status = proxy.ConvertSimple(accountID=name, password=passwd, TTStext=xlatText)
#print status
return status
def textToSpeech2(name, passwd, xlatText):
server = SOAPProxy(url, namespace)
server.config.debug = 0
reqStatus = server.ConvertSimple(name, passwd, xlatText)
print "request status = ", reqStatus
retResult = reqStatus.split('&')
print "request result = " , retResult
if int(retResult[0]) == 0 :
# check convert progress
cvtResult = ['0'] * 5
#statusCode = '0'
while int(cvtResult[2]) in {0, 1}: # wait completion
cvtStatus = server.GetConvertStatus(name, passwd, int(retResult[2]))
print "convert status = ", cvtStatus, "ID", retResult[2]
# on success, status is return URL
#resultCode, resultString, statusCode, status = cvtStatus.split('&')
cvtResult = cvtStatus.split('&')
print cvtResult
sleep(0.5)
# save to file
if (int(cvtResult[2]) == 2): # '2' means "completed"
waveUrl = cvtResult[4]
req = urllib2.Request(waveUrl)
resp = urllib2.urlopen(req)
wavFile = resp.read()
outfile = open(xlatText + u".wav", 'wb')
outfile.write(wavFile)
else:
return retResult[1]
class Usage(Exception):
def __init__(self, msg):
self.msg = msg
def main(argv=None):
if argv is None:
argv = sys.argv
try:
try:
opts, args = getopt.getopt(argv[1:], "a:p:t:h", ["account", "password", "text", "help"])
except getopt.error, msg:
raise Usage(msg)
# clean account information
account = password = textMsg = u""
for opt, arg in opts:
if opt in ('-a', '--account'):
account = arg
elif opt in ('-p', '--password'):
password = arg
elif opt in ('-t', '--text'):
print "sys encoding = ", sys.getdefaultencoding()
print "arg type -->", type(arg)
# in windows (zh-TW), the default encoding of command shell is 'ascii'.
# for chinese input, the encoding could be 'big5' or 'mbcs'
textMsg = unicode(arg, sys.getfilesystemencoding())
elif opt in ('-h', '--help'):
print >> sys.stderr, "python ttstest.py -a account -p password -t message"
return 0
if (textMsg != u""):
status = textToSpeech2(account, password, textMsg)
print status
else:
raise Usage("message is not specified !")
except Usage, err:
print >> sys.stderr, err.msg
print >> sys.stderr, "for help use --help"
return 2
if __name__ == "__main__":
import sys
import getopt
#reload(sys)
#sys.setdefaultencoding('utf8')
sys.exit(main())
print status