updated
This commit is contained in:
@@ -40,6 +40,7 @@ local function receiveLoop()
|
||||
elseif message.type == "chat" then
|
||||
print(message.username .. ": " .. message.content)
|
||||
speaker.play("notification")
|
||||
speaker.playTTSFile(message.content)
|
||||
end
|
||||
|
||||
write("> ")
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
local speaker = peripheral.find("speaker")
|
||||
local dfpwm = require("cc.audio.dfpwm")
|
||||
|
||||
local encoder = dfpwm.make_encoder()
|
||||
local decoder = dfpwm.make_decoder()
|
||||
|
||||
local sounds = {
|
||||
{name = "notification", file = "notification.dfpwm"}
|
||||
@@ -49,5 +50,49 @@ local function play(name)
|
||||
end
|
||||
return true
|
||||
end
|
||||
----------------
|
||||
|
||||
return { initialize = initialize, play = play }
|
||||
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(fileName)
|
||||
local fileStream = getFileName(fileName)
|
||||
local values = io.lines(fileStream, 16 * 1024)
|
||||
for input in values do
|
||||
local decoded = decoder(input)
|
||||
while not speaker.playAudio(decoded, 3.0) do
|
||||
os.pullEvent("speaker_audio_empty")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
local function playTTSFile(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(name)
|
||||
shell.execute("rm", fileName)
|
||||
end
|
||||
|
||||
return { initialize = initialize, play = play, playTTSFile = playTTSFile }
|
||||
|
||||
Reference in New Issue
Block a user