Files
CC-StationBroadcast/station_broadcast-warning.lua

123 lines
3.5 KiB
Lua

-- Random Audio Player
-- Simplified version that only plays random audio files
local COLORS = {
INFO = colors.white,
WARN = colors.yellow,
WARNING = colors.yellow,
ERROR = colors.red,
DEBUG = colors.cyan,
SUCCESS = colors.green
}
local dfpwm = require "cc.audio.dfpwm"
-- Debug output function
local function debugPrint(level, message)
local timestamp = os.date("%H:%M:%S")
local logMessage = string.format("[%s][%s] %s", timestamp, level, message)
local color = COLORS[level] or colors.white
local oldColor = term.getTextColor()
term.setTextColor(color)
print(logMessage)
term.setTextColor(oldColor)
end
local function playDfpwmFile(path, volume)
local speakers = { peripheral.find("speaker") }
if #speakers == 0 then
debugPrint("ERROR", "No speakers found")
return false
end
local file = fs.open(path, "rb")
if not file then
debugPrint("ERROR", "Cannot open audio file: " .. path)
return false
end
local decoder = dfpwm.make_decoder()
local controller = speakers[1]
while true do
local chunk = file.read(16 * 1024)
if not chunk then break end
local samples = decoder(chunk)
if controller and type(controller.playAudioAll) == "function" then
while not controller.playAudioAll(samples, volume) do
local timerId = os.startTimer(0.05)
while true do
local ev = { os.pullEvent() }
if ev[1] == "timer" and ev[2] == timerId then
break
elseif ev[1] == "speaker_audio_empty" then
break
end
end
end
else
local pending = {}
for _, sp in ipairs(speakers) do
pending[peripheral.getName(sp)] = sp
end
local maxAttempts = 20
while next(pending) do
for name, sp in pairs(pending) do
if sp.playAudio(samples, volume) then
pending[name] = nil
end
end
if next(pending) then
maxAttempts = maxAttempts - 1
if maxAttempts <= 0 then
break
end
local timerId = os.startTimer(0.05)
while true do
local ev = { os.pullEvent() }
if ev[1] == "timer" and ev[2] == timerId then
break
elseif ev[1] == "speaker_audio_empty" then
break
end
end
end
end
end
end
file.close()
return true
end
-- Add random audio playback function
local function playRandomAudio()
local audioFiles = {"aqtx", "jzxy"}
local randomFile = audioFiles[math.random(#audioFiles)]
local path = "/Audio/"..randomFile..".dfpwm"
if fs.exists(path) then
debugPrint("INFO", "Attempting to play: "..path)
playDfpwmFile(path, 3.0)
debugPrint("INFO", "Finished playing: "..randomFile)
else
debugPrint("ERROR", "Audio file not found: "..path)
end
end
-- Main loop
local elapsedSeconds = 0
while true do
if elapsedSeconds >= 240 then
playRandomAudio()
elapsedSeconds = 0
end
os.sleep(1)
elapsedSeconds = elapsedSeconds + 1
end