-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathtts.agent.nut
46 lines (42 loc) · 1.14 KB
/
tts.agent.nut
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
data <- {
key="****************************",
command="produce",
lang="en_us",
voice="Male01",
text="hello",
audioformat="alaw",
container="none",
samplerate=16000,
sampledepth=8
}
function GetAndAutoRedirect(url, cb) {
server.log("GET " + url);
http.get(url).sendasync(function(resp) {
// if we got a redirect
if(resp.statuscode == 302) {
if ("location" in resp.headers) {
server.log("Trying " + resp.headers.location + " in 5 seconds");
GetAndAutoRedirect(resp.headers.location, cb);
}
} else {
cb(resp);
}
});
}
function say(text) {
data["text"] = text;
url <- format("http://tts.readspeaker.com/a/speak?%s", http.urlencode(data));
GetAndAutoRedirect(url, function(resp) {
if (resp.statuscode == 200) {
device.send("audio", resp.body);
} else {
server.log(format("ERROR %i: %s", resp.statuscode, resp.body));
}
});
}
http.onrequest(function(req, resp) {
if ("say" in req.query) {
say(req.query.say);
}
resp.send(200, "OK");
});