Create POST /{format} endpoint (file.lua)

This commit is contained in:
2020-07-06 20:59:23 +02:00
parent abe02d4f02
commit fde05671d3
5 changed files with 45 additions and 5 deletions

View File

@@ -27,7 +27,8 @@ class TTSLibrary(private val ttsEngine: TTSEngine) : TwoArgFunction() {
class SayMethod(private val ttsEngine: TTSEngine) : TwoArgFunction() {
override fun call(text: LuaValue, language: LuaValue): LuaValue {
ttsEngine.performTTS(text.checkjstring(), Locale.forLanguageTag(language.checkjstring()))
val locale = Locale.forLanguageTag(language.checkjstring().toUpperCase(Locale.ROOT))
ttsEngine.performTTS(text.checkjstring(), locale)
return LuaValue.NIL
}

View File

@@ -49,7 +49,8 @@ class ScriptsInitializer(private val context: Context, private val preferences:
companion object {
private const val INITIALIZED_FLAG = "flag_initialized"
private val endpoints = mapOf(
"say.lua" to R.raw.say
"say.lua" to R.raw.say,
"file.lua" to R.raw.file
)
}
}

View File

@@ -1,2 +1,19 @@
return {
local silenceModeTimeRange = {
from = { 23, 00 },
to = { 07, 00 }
}
function silenceMode()
local date = os.date("*t")
local from = silenceModeTimeRange.from[1] * 60 + silenceModeTimeRange.from[2]
local to = silenceModeTimeRange.to[1] * 60 + silenceModeTimeRange.to[2]
local now = date.hour * 60 + date.min
if (from <= to) then return from <= now and now <= to
else return from <= now or now <= to end
end
return {
silenceMode = silenceMode
}

View File

@@ -0,0 +1,18 @@
return {
uri = "/{format}",
method = Method.POST,
accepts = Mime.JSON,
consumer = function(request)
local body = json.decode(request.body)
local format = (request.params.format or "WAV"):upper()
local audioFormat = AudioFormat[format]
local mime = Mime[format]
local file = tts.sayToFile(body.text, body.language or "en", audioFormat)
return {
mime = mime,
data = file
}
end
}

View File

@@ -2,8 +2,11 @@ return {
uri = "/say",
method = Method.POST,
queued = true,
accepts = "application/json",
consumer = function()
tts.say("Hello, world!", "en")
accepts = Mime.JSON,
consumer = function(request)
if(config.silenceMode()) then return end
local body = json.decode(request.body)
tts.say(body.text, body.language or "en")
end
}