Files
CC-StationBroadcast/station_broadcast-warning.lua
T
Henry_Du c73bceaec7 feat: 重构音频系统,移除外部依赖并新增WAV格式支持
- 移除install和update脚本中的aukit/austream下载配置与目标,不再部署辅助脚本
- 替换station_broadcast.lua的aukit库为CC内置dfpwm模块,重写音频播放逻辑
- 添加原生扬声器API支持,支持WAV格式音频播放
- 重写云端服务器音频处理逻辑,支持直接传输WAV文件无需预转换
- 新增WAV文件时长解析、音频序列合成等辅助功能
2026-07-31 19:29:38 +08:00

101 lines
2.7 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
os.pullEvent("speaker_audio_empty")
end
else
local pending = {}
for _, sp in ipairs(speakers) do
pending[peripheral.getName(sp)] = sp
end
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
os.pullEvent("speaker_audio_empty")
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