-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspeech.py
62 lines (49 loc) · 2.23 KB
/
speech.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
# Project Oxford speech authorization module
import urllib.parse, http.client, json
def auth(clientId, clientSecret):
"""Retrieves access token to set up an authorized Project Oxford speech API session
"""
# Setup REST API call details
ttsHost = "https://speech.platform.bing.com"
params = urllib.parse.urlencode({'grant_type': 'client_credentials', 'client_id': clientId,
'client_secret': clientSecret, 'scope': ttsHost})
headers = {"Content-type": "application/x-www-form-urlencoded"}
AccessTokenHost = "oxford-speech.cloudapp.net"
path = "/token/issueToken"
# Connect to REST API to get the Oxford Access Token
conn = http.client.HTTPSConnection(AccessTokenHost)
conn.request("POST", path, params, headers)
response = conn.getresponse()
# Retrieve the response to local data variable
data = response.read()
conn.close()
accesstoken = data.decode("UTF-8")
#decode the token object from json
ddata=json.loads(accesstoken)
access_token = ddata['access_token']
# Return the token
return access_token;
def synthesize(access_token, clientId, text_to_synthesize):
"""Converts text to a WAV data stream that can be written to a file
"""
# Setup REST API call details (note used of Australian voice request in the body)
headers = {"Content-type": "application/ssml+xml",
"Authorization": "Bearer " + access_token,
"X-Microsoft-OutputFormat": "riff-8khz-8bit-mono-mulaw",
"User-Agent": clientId}
body="""<speak version="1.0" xmlns="http://www.w3.org/2001/10/synthesis" \
xmlns:mstts="http://www.w3.org/2001/mstts" xml:lang="en-AU">\
<voice name="Microsoft Server Speech Text to Speech Voice (en-AU, Catherine)">"""
body = body + text_to_synthesize
body = body + '</voice></speak>'
# Connect to REST API to get synthesized audio
conn = http.client.HTTPSConnection("speech.platform.bing.com")
conn.request("POST", "/synthesize", body, headers)
response = conn.getresponse()
if response.status != 200:
conn.close()
return 0
# read response data and return to caller
responsedata = response.read()
conn.close()
return responsedata