From 06314ba26a09e32d75cf6d23c7cccb85ac41dee5 Mon Sep 17 00:00:00 2001 From: hkc Date: Sat, 6 Jan 2024 14:15:14 +0300 Subject: [PATCH] Removed unnecessarry stuff --- stream.lua | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/stream.lua b/stream.lua index e2504d6..a0f57f6 100644 --- a/stream.lua +++ b/stream.lua @@ -1,7 +1,6 @@ local args = { ... } local dfpwm = require("cc.audio.dfpwm") -local buffer_size = 8192 if not http then print("no http, check config") @@ -22,26 +21,35 @@ if not req then return end -local buffer = { } -for i = 1, buffer_size do - buffer[i] = 0 +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 decode = dfpwm.make_decoder() +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(buffer_size) + local chunk = req.read(16384) if not chunk then break end - --local buffer = {} - --for i = 1, #chunk do - -- buffer[i] = string.byte(chunk, i) - 128 - --end - local buffer = decode(chunk) - while not speaker.playAudio(buffer) do os.pullEvent("speaker_audio_empty") end end + +print()