feat(music): stream live wav chunks to HQ speakers
Slice realtime WAV audio into small self-contained chunks on the server and feed them into the HQ speaker WAV queue on stations so music can start playing before the full file finishes transferring.
This commit is contained in:
+98
-1
@@ -796,6 +796,49 @@ local function playHqAudioItem(item)
|
||||
end
|
||||
end
|
||||
|
||||
local function pushWavChunkToSpeakers(payload, playSeq)
|
||||
local controller = Peripherals.speakers and Peripherals.speakers[1] or nil
|
||||
if controller and type(controller.speakWavAll) == "function" then
|
||||
while true do
|
||||
if shouldStop(playSeq) then return false end
|
||||
local ok = false
|
||||
pcall(function()
|
||||
ok = controller.speakWavAll(payload, CONFIG.AUDIO_VOLUME)
|
||||
end)
|
||||
if ok then
|
||||
return true
|
||||
end
|
||||
if not waitForInterruptibleDelay(playSeq, 0.05) then
|
||||
return false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local pending = {}
|
||||
for _, sp in ipairs(Peripherals.speakers or {}) do
|
||||
local name = peripheral.getName(sp)
|
||||
if name then pending[name] = sp end
|
||||
end
|
||||
|
||||
while next(pending) do
|
||||
if shouldStop(playSeq) then return false end
|
||||
for name, sp in pairs(pending) do
|
||||
local ok = false
|
||||
pcall(function()
|
||||
if type(sp.speakWav) == "function" then
|
||||
ok = sp.speakWav(payload, CONFIG.AUDIO_VOLUME)
|
||||
end
|
||||
end)
|
||||
if ok then pending[name] = nil end
|
||||
end
|
||||
if next(pending) and not waitForInterruptibleDelay(playSeq, 0.05) then
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
return true
|
||||
end
|
||||
|
||||
function Audio.playDfpwmItem(item)
|
||||
if not Peripherals.speakers or #Peripherals.speakers == 0 then
|
||||
Utils.log("WARN", "No speakers found; cannot play audio")
|
||||
@@ -868,12 +911,64 @@ function Audio.playPcmItem(item)
|
||||
end
|
||||
end
|
||||
|
||||
function Audio.playWavStreamItem(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
|
||||
Audio.stop("current")
|
||||
return
|
||||
end
|
||||
|
||||
local chunk = source()
|
||||
if chunk == nil then break end
|
||||
if type(chunk) == "string" and #chunk > 0 then
|
||||
if not pushWavChunkToSpeakers(chunk, playSeq) then
|
||||
Audio.stop("current")
|
||||
return
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
while areHqSpeakersBusy(Peripherals.speakers) do
|
||||
if shouldStop(playSeq) then
|
||||
Audio.stop("current")
|
||||
return
|
||||
end
|
||||
if not waitForInterruptibleDelay(playSeq, 0.05) then
|
||||
Audio.stop("current")
|
||||
return
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function Audio.playItem(item)
|
||||
local format = tostring(item.audioFormat or "dfpwm"):lower()
|
||||
if format == "wav" or format == "file" or format == "stream" or format == "hls" then
|
||||
return playHqAudioItem(item)
|
||||
elseif format == "pcm" then
|
||||
return Audio.playPcmItem(item)
|
||||
elseif format == "wav-stream" then
|
||||
return Audio.playWavStreamItem(item)
|
||||
end
|
||||
return Audio.playDfpwmItem(item)
|
||||
end
|
||||
@@ -1084,11 +1179,13 @@ function Network.messageLoop()
|
||||
audioId = data.audioId
|
||||
})
|
||||
State.audioRx = nil
|
||||
elseif audioFormat == "dfpwm" or audioFormat == "pcm" then
|
||||
elseif audioFormat == "dfpwm" or audioFormat == "pcm" or audioFormat == "wav-stream" then
|
||||
local stream = Audio.createStream()
|
||||
State.audioRx.stream = stream
|
||||
if audioFormat == "pcm" then
|
||||
Utils.log("INFO", "Receiving live PCM audio stream")
|
||||
elseif audioFormat == "wav-stream" then
|
||||
Utils.log("INFO", "Receiving live WAV chunk stream")
|
||||
end
|
||||
Audio.enqueueItem({
|
||||
audioData = stream,
|
||||
|
||||
Reference in New Issue
Block a user