From 6b1a531b77e74530180abd6a1714fe160fec02a6 Mon Sep 17 00:00:00 2001 From: HenryDu8133 <813367384@qq.com> Date: Sat, 15 Aug 2026 23:30:09 +0800 Subject: [PATCH] =?UTF-8?q?fix(audio):=20=E4=BF=AE=E5=A4=8D=E5=9C=A8?= =?UTF-8?q?=E6=B2=A1=E6=9C=89=E9=AB=98=E9=9F=B3=E8=B4=A8=E6=A8=A1=E5=9D=97?= =?UTF-8?q?=E6=97=B6=EF=BC=8C=E5=9B=9E=E9=80=80=E6=92=AD=E6=94=BE=E5=AE=9E?= =?UTF-8?q?=E6=97=B6=E9=9F=B3=E9=A2=91=E6=B5=81=E4=BA=A7=E7=94=9F=E5=88=BA?= =?UTF-8?q?=E8=80=B3=E5=99=AA=E9=9F=B3=E5=8F=8A=E5=8F=AF=E8=83=BD=E5=AF=BC?= =?UTF-8?q?=E8=87=B4=E5=8D=A1=E6=AD=BB=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- station_broadcast.lua | 56 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 53 insertions(+), 3 deletions(-) diff --git a/station_broadcast.lua b/station_broadcast.lua index 1865fee..170ae00 100644 --- a/station_broadcast.lua +++ b/station_broadcast.lua @@ -1296,6 +1296,21 @@ local function decodePcmS8Chunk(chunk) return samples end +local function decodePcmS16LEChunk(chunk) + if type(chunk) ~= "string" or #chunk < 2 then return nil end + local samples = {} + local idx = 1 + for i = 2, #chunk, 2 do + local b = string.byte(chunk, i) + if b >= 128 then + b = b - 256 + end + samples[idx] = b + idx = idx + 1 + end + return samples +end + local function readU16LE(data, offset) local a, b = string.byte(data, offset, offset + 1) if not a or not b then return nil end @@ -1684,6 +1699,41 @@ function Audio.playPcmItem(item) end end +function Audio.playPcmS16LEItem(item) + if not Peripherals.speakers or #Peripherals.speakers == 0 then + Utils.log("WARN", "No speakers found; cannot play audio") + return + end + + local playSeq = State.interruptSeq + local source = item.audioData + + if type(source) == "table" and source.read then + source = source.read + end + + if type(source) ~= "function" then + local data = source + source = function() + local chunk = data + data = nil + return chunk + end + end + + while true do + if shouldStop(playSeq) then break end + local chunk = source() + if chunk == nil then break end + if type(chunk) == "string" and #chunk > 0 then + local samples = decodePcmS16LEChunk(chunk) + if samples and not playChunkOnSpeakers(samples, CONFIG.AUDIO_VOLUME, playSeq) then + break + end + end + end +end + function Audio.playWavStreamItem(item) if not Peripherals.speakers or #Peripherals.speakers == 0 then Utils.log("WARN", "No speakers found; cannot play audio") @@ -1788,7 +1838,7 @@ function Audio.playItem(item) if success == false then format = tostring(item.audioFormat or "file"):lower() Utils.log("WARN", "HQ speaker playback failed, falling back to default decoding for format: " .. format) - if format == "wav" or format == "file" then + if format == "wav" or format == "file" or format == "stream" then return Audio.playWavStreamItem(item) elseif format == "dfpwm" then return Audio.playDfpwmItem(item) @@ -1800,8 +1850,8 @@ function Audio.playItem(item) return success elseif format == "pcm" then return Audio.playPcmItem(item) - elseif format == "wav-stream" then - return Audio.playWavStreamItem(item) + elseif format == "wav-stream" or format == "wav-stream-live" then + return Audio.playPcmS16LEItem(item) end return Audio.playDfpwmItem(item) end