56 lines
1.1 KiB
Lua
56 lines
1.1 KiB
Lua
local args = { ... }
|
|
|
|
local dfpwm = require("cc.audio.dfpwm")
|
|
|
|
if not http then
|
|
print("no http, check config")
|
|
return
|
|
end
|
|
|
|
local speaker = peripheral.find("speaker")
|
|
|
|
if not speaker then
|
|
print("no speaker")
|
|
return
|
|
end
|
|
|
|
local req, err = http.get(args[1], {}, true)
|
|
if not req then
|
|
print("failed to perform HTTP request")
|
|
print(err)
|
|
return
|
|
end
|
|
|
|
local headers = req.getResponseHeaders()
|
|
local length = tonumber(headers["Content-Length"]) or 0
|
|
|
|
local function decode_s8(data)
|
|
local buffer = {}
|
|
for i = 1, #data do
|
|
local v = string.byte(data, i)
|
|
if bit32.band(v, 0x80) then
|
|
v = bit32.bxor(v, 0x7F) - 128
|
|
end
|
|
buffer[i] = v
|
|
end
|
|
return buffer
|
|
end
|
|
|
|
local use_dfpwm = ({ args[1]:find("%.dfpwm") })[2] == #args[1]
|
|
|
|
local decode = use_dfpwm and dfpwm.make_decoder() or decode_s8
|
|
local read_bytes = 0
|
|
while true do
|
|
local chunk = req.read(16384)
|
|
if not chunk then
|
|
break
|
|
end
|
|
|
|
local buffer = decode(chunk)
|
|
while not speaker.playAudio(buffer) do
|
|
os.pullEvent("speaker_audio_empty")
|
|
end
|
|
end
|
|
|
|
print()
|