-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtts.lua
More file actions
66 lines (54 loc) · 1.84 KB
/
Copy pathtts.lua
File metadata and controls
66 lines (54 loc) · 1.84 KB
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
-------------------------------------------------------------------------------
-- TTS support using Google TTS API
-------------------------------------------------------------------------------
require('config')
local http = require('socket.http')
tts = {}
tts.sources = {}
-- Taken from https://gist.github.com/ignisdesign/4323051
-- Thanks!
local function urlencode(str)
if (str) then
str = string.gsub (str, "\n", "\r\n")
str = string.gsub (str, "([^%w ])",
function (c) return string.format ("%%%02X", string.byte(c)) end)
str = string.gsub (str, " ", "+")
end
return str
end
local function download_file(string, file_name)
local file = love.filesystem.newFile(file_name)
file:open("w")
http.request{
url = "https://translate.google.com/translate_tts?ie=UTF-8&q=" .. urlencode(string) .. "&tl=en&client=tw-ob",
headers = {
"Referer: http://translate.google.com/",
"User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:57.0) Gecko/20100101 Firefox/57.0"
},
sink = ltn12.sink.file(file)
}
file:close()
end
-- Say the given string
function tts:say(string)
if (not config.enable_tts) then
return
end
if (not tts.sources[string]) then
if (not love.filesystem.exists("cache")) then
love.filesystem.createDirectory("cache")
end
local file_name = "cache/" .. string:gsub("%s", "_") .. ".mp3"
if (not love.filesystem.exists(file_name)) then
download_file(string, file_name)
end
tts.sources[string] = love.audio.newSource(file_name, "static")
end
tts.sources[string]:play()
end
-- Stop playing the specified string, if it is being played
function tts:stop_saying(string)
if (tts.sources[string]) then
tts.sources[string]:stop()
end
end