forked from hkc/cc-stuff
1
0
Fork 0
cc-stuff/stream.lua

56 lines
1.1 KiB
Lua
Raw Normal View History

2023-10-05 02:06:32 +03:00
local args = { ... }
2023-12-18 18:23:11 +03:00
local dfpwm = require("cc.audio.dfpwm")
2023-10-05 02:06:32 +03:00
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
2024-01-06 14:15:14 +03:00
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
2023-10-05 02:06:32 +03:00
end
2024-01-06 14:15:14 +03:00
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
2023-10-05 02:06:32 +03:00
while true do
2024-01-06 14:15:14 +03:00
local chunk = req.read(16384)
2023-10-05 02:06:32 +03:00
if not chunk then
break
end
2023-12-18 18:23:11 +03:00
2023-12-18 18:29:28 +03:00
local buffer = decode(chunk)
2023-10-05 02:06:32 +03:00
while not speaker.playAudio(buffer) do
os.pullEvent("speaker_audio_empty")
end
end
2024-01-06 14:15:14 +03:00
print()