170 lines
4.0 KiB
Lua
170 lines
4.0 KiB
Lua
local dfpwm = require("cc.audio.dfpwm")
|
||
local encoder = dfpwm.make_encoder()
|
||
local decoder = dfpwm.make_decoder()
|
||
local baseRoute = "https://git.astrocore.space/root/NovaCorpLLC/raw/branch/main/"
|
||
|
||
local function getFileName(name)
|
||
local extension = ".dfpwm"
|
||
local fullFile = name .. extension
|
||
return fullFile
|
||
end
|
||
|
||
local function randomFileName()
|
||
local name = ""
|
||
for i = 1, 12 do
|
||
name = name .. string.char(math.random(97, 122)) -- a–z
|
||
end
|
||
return name
|
||
end
|
||
|
||
-- local function playSound(speaker, fileName)
|
||
-- local fileStream = getFileName(fileName)
|
||
-- local values = io.lines(fileStream, 16 * 1024)
|
||
-- for input in values do
|
||
-- print("playing audo....")
|
||
-- local decoded = decoder(input)
|
||
|
||
|
||
-- while not speaker.playAudio(decoded, 3) do
|
||
-- os.pullEvent("speaker_audio_empty")
|
||
-- end
|
||
-- end
|
||
-- end
|
||
|
||
local function playFullFile(speaker, fileName)
|
||
local decoder = require("cc.audio.dfpwm").make_decoder()
|
||
local file = fs.open(fileName, "rb")
|
||
|
||
if not file then
|
||
print("Failed to open file: " .. fileName)
|
||
return
|
||
end
|
||
|
||
while true do
|
||
local chunk = file.read(16 * 1024)
|
||
if not chunk then break end
|
||
|
||
local decoded = decoder(chunk)
|
||
while not speaker.playAudio(decoded, 3) do
|
||
os.pullEvent("speaker_audio_empty")
|
||
end
|
||
end
|
||
|
||
file.close()
|
||
end
|
||
|
||
local function playSound(speakers, fileName)
|
||
local tasks = {}
|
||
for _, speaker in ipairs(speakers) do
|
||
table.insert(tasks, function()
|
||
playFullFile(speaker, fileName)
|
||
end)
|
||
end
|
||
|
||
parallel.waitForAll(table.unpack(tasks))
|
||
end
|
||
|
||
|
||
|
||
|
||
local function playControlRoomAlarm(speakers)
|
||
playSound(speakers,"short_control_alarm")
|
||
end
|
||
|
||
local function playExternalAlarm(speakers)
|
||
playSound(speakers,"external_alarm")
|
||
end
|
||
local function playInternalAlarm(speakers)
|
||
playSound(speakers,"internal_alarm")
|
||
end
|
||
|
||
local function playTTSFile(speakers, value)
|
||
local message = textutils.urlEncode(value)
|
||
local url = "http://api.astrocore.space/api/TextToSpeech?message=" .. message
|
||
local response, err = http.get { url = url, binary = true }
|
||
local name = randomFileName()
|
||
local fileName = name .. ".dfpwm"
|
||
|
||
local fileData = response.readAll()
|
||
local file = fs.open(fileName,"w")
|
||
file.write(fileData)
|
||
file.close()
|
||
response.close()
|
||
playSound(speakers, name)
|
||
shell.execute("rm", fileName)
|
||
end
|
||
|
||
local function createSoundFile(fileName)
|
||
local name = getFileName(fileName)
|
||
local fullPath = baseRoute .. name
|
||
shell.execute("rm", name)
|
||
local response = http.request({
|
||
url = fullPath,
|
||
method = "GET",
|
||
binary = true
|
||
})
|
||
|
||
|
||
local event, url, handle
|
||
repeat
|
||
event, url, handle = os.pullEvent()
|
||
if event == "http_failure" and url == fullPath then
|
||
print("HTTP request failed for: " .. url)
|
||
print(event)
|
||
return nil
|
||
end
|
||
if event == "http_success" then
|
||
print("Waiting for response for " .. fileName)
|
||
end
|
||
until event == "http_success" and url == fullPath
|
||
print("Recieved response!")
|
||
local fileData = handle.readAll()
|
||
local file = fs.open(name,"w")
|
||
|
||
file.write(fileData)
|
||
|
||
file.close()
|
||
handle.close()
|
||
|
||
end
|
||
|
||
|
||
|
||
function addSoundFile(fileName)
|
||
createSoundFile(fileName)
|
||
end
|
||
|
||
local function watch()
|
||
end
|
||
|
||
|
||
local function startup()
|
||
local sounds = {
|
||
{ fileName = "short_control_alarm" },
|
||
{ fileName = "external_alarm" },
|
||
{ fileName = "internal_alarm" }
|
||
}
|
||
|
||
for i, item in ipairs(sounds) do
|
||
addSoundFile(item.fileName)
|
||
print("Added sound file: " .. item.fileName)
|
||
end
|
||
end
|
||
|
||
local function shutdown()
|
||
end
|
||
|
||
|
||
local function report()
|
||
end
|
||
|
||
return { report = report,
|
||
watch = watch,
|
||
startup = startup,
|
||
shutdown = shutdown,
|
||
playControlRoomAlarm = playControlRoomAlarm,
|
||
addSoundFile = addSoundFile,
|
||
playExternalAlarm = playExternalAlarm,
|
||
playInternalAlarm = playInternalAlarm,
|
||
playTTSFile = playTTSFile
|
||
} |