metastruct-experiments/starfallex/starfall_tts_gimpy.lua

91 lines
2.3 KiB
Lua
Raw Normal View History

2022-05-02 17:31:40 +00:00
--@name GimpyTTS
--@author Mavain, Minori, Henke, et al.
2022-05-02 17:31:40 +00:00
--@client
2022-06-02 14:33:54 +00:00
local VALID_LANGS = {"en-gb", "en-ca", "en-us", "en-au", "ja", "ph", "so"}
local errorLookup = {[2] = "Invalid language"}
2022-06-02 14:33:54 +00:00
2022-05-17 15:33:32 +00:00
local lang = "en-gb"
local curLang = lang
2022-05-02 17:31:40 +00:00
local soundref
-- Check if client has permission
if not hasPermission("bass.loadURL", "https://translate.google.com/translate_tts") then return end
2022-06-02 14:33:54 +00:00
-- A local function to check if this exists on our valid langs
-- if it doesn't, we throw error
local function has_value (tab, val)
for index, value in ipairs(tab) do
if value == val then
return true
end
end
return false
end
local function RequestTTS(txt, l, callback)
bass.loadURL("https://translate.google.com/translate_tts?ie=UTF-8&q=" .. txt .. "&tl=" .. curLang .. "&client=tw-ob", "3d", callback)
end
local function DoTTS(sound)
-- we dispose the current reference, then we create a new one
if soundref then soundref:stop() end
soundref = sound
--local soundLength = sound:getLength()
hook.add("think", "soundFollow", function()
if sound:isValid() then sound:setPos(owner():getPos()) else
hook.remove("think", "soundFollow")
end
end)
sound:play()
end
hook.add("playerchat", "fucke2", function(ply, txt)
2022-05-02 17:31:40 +00:00
if ply ~= owner() then return end
if string.sub(txt, 1, 1) == ":" then
local l = string.gsub(txt, ":", "")
if #l > 1 then
local lastLang = l
if has_value(VALID_LANGS, l) then
lang = l
else
print("Invalid parameter, check source for valid languages.")
2022-06-22 00:57:48 +00:00
-- Do not error, just reassign back the last language selection
lang = lastLang
end
end
end
2022-05-15 01:56:25 +00:00
if string.sub(txt, 1, 1) ~= ";" then return end
txt = string.sub(txt,2)
if #txt < 1 then return end
txt = http.urlEncode(txt)
2022-05-15 01:56:25 +00:00
RequestTTS(txt, curLang, function(sound, err, name)
if not sound then
print("error: " .. errorLookup[err])
if err == 2 then
lang = curLang
RequestTTS(txt, curLang, function(s, e, n)
if not sound then return end
DoTTS(sound)
end)
end
return
end
DoTTS(sound)
end)
end)