Created
August 14, 2022 12:57
-
-
Save x0rnn/cd08ab7f1ed09cd5d8d177c27244577f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- tts.py by x0rnn, primitive TTS (text-to-speech) system based on arpabet (https://en.wikipedia.org/wiki/Arpabet) | |
-- use with https://github.com/x0rnn/etpro/blob/master/lua/tts.pk3 and https://github.com/x0rnn/etpro/blob/master/lua/tts.txt | |
filename = "luascripts/tts.txt" | |
dict = {} | |
arpabet = "" | |
timers = {} | |
steps = {} | |
function et_InitGame(levelTime, randomSeed, restart) | |
et.RegisterModname("tts.lua "..et.FindSelf()) | |
end | |
function et_RunFrame(lvltime) | |
levelTime = lvltime | |
for i, timer in pairs(timers) do | |
if timer[1] <= levelTime then | |
et.G_globalSound("sound/tts/" .. string.lower(timer[2]) .. ".wav") | |
local step = steps[1] | |
table.remove(steps, 1) | |
if steps[1] == "XX" then | |
table.remove(timers, 1) | |
table.remove(steps, 1) | |
else | |
timer[2] = step | |
timer[1] = levelTime + 180 | |
end | |
end | |
end | |
end | |
function readDict(args_table, id) | |
arpabet = "" | |
local wordcount = #args_table | |
if next(dict) == nil then | |
local fd,len = et.trap_FS_FOpenFile(filename, et.FS_READ) | |
if len == -1 then | |
et.G_Print("tts.lua: no tts.txt\n") | |
return(0) | |
end | |
local filestr = et.trap_FS_Read(fd, len) | |
et.trap_FS_FCloseFile(fd) | |
local word, arpa | |
for word, arpa in string.gmatch(filestr,"([^\n]+)\t([^\n]+)") do | |
dict[word] = | |
{ | |
arpa | |
} | |
end | |
end | |
if next(dict) ~= nil then | |
local cnt = 0 | |
for i = 1, wordcount do | |
if type(dict[args_table[i]]) == "table" then | |
cnt = cnt + 1 | |
arpabet = arpabet .. dict[args_table[i]][1] .. " " | |
end | |
end | |
if cnt ~= wordcount then | |
arpabet = "" | |
local missing = "" | |
for i = 1, wordcount do | |
if type(dict[args_table[i]]) ~= "table" then | |
missing = missing .. args_table[i] .. " " | |
end | |
end | |
et.trap_SendServerCommand(id, "chat \"^7Didn't find the following words: ^3" .. string.sub(missing, 1, -2) .. "^7, can't do TTS.\"\n") | |
end | |
if arpabet ~= "" then | |
local arpa_table = {} | |
cnt = 0 | |
for i in string.gmatch(arpabet, "%S+") do | |
table.insert(arpa_table, i) | |
cnt = cnt + 1 | |
end | |
for i = 1, #arpa_table do | |
if i > 1 then | |
table.insert(steps, arpa_table[i]) | |
end | |
end | |
table.insert(steps, "X") | |
table.insert(steps, "XX") | |
table.insert(timers, {levelTime + 180, arpa_table[1]}) | |
end | |
end | |
end | |
function et_ClientCommand(id, command) | |
if et.trap_Argv(0) == "say" then | |
args = et.ConcatArgs(1) | |
local args_table = {} | |
cnt = 0 | |
for i in string.gmatch(args, "%S+") do | |
table.insert(args_table, string.lower(i)) | |
cnt = cnt + 1 | |
end | |
if args_table[1] ~= nil and args_table[1] == "!tts" then | |
if cnt == 1 then | |
et.trap_SendServerCommand(id, "chat \"Usage: ^7!tts <^3bla bla^7>\"\n") | |
else | |
table.remove(args_table, 1) | |
readDict(args_table, id) | |
end | |
end | |
end | |
return(0) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment