From e61fa4023b745ea61d267cb2a4893005ef6f3a90 Mon Sep 17 00:00:00 2001 From: hkc Date: Thu, 5 Oct 2023 02:06:32 +0300 Subject: [PATCH] Raw audio streaming --- stream.lua | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 stream.lua diff --git a/stream.lua b/stream.lua new file mode 100644 index 0000000..0839fd5 --- /dev/null +++ b/stream.lua @@ -0,0 +1,41 @@ +local args = { ... } + +local buffer_size = 8192 + +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 buffer = { } +for i = 1, buffer_size do + buffer[i] = 0 +end + +while true do + local chunk = req.read(buffer_size) + if not chunk then + break + end + buffer = {} + for i = 1, #chunk do + buffer[i] = string.byte(chunk, i) - 128 + end + while not speaker.playAudio(buffer) do + os.pullEvent("speaker_audio_empty") + end +end