54 lines
1.2 KiB
Lua
54 lines
1.2 KiB
Lua
local speaker = peripheral.find("speaker")
|
|
local dfpwm = require("cc.audio.dfpwm")
|
|
|
|
|
|
local sounds = {
|
|
{name = "notification", file = "notification.dfpwm"}
|
|
}
|
|
|
|
local function initialize()
|
|
kernel = require("kernel")
|
|
kernel.addSound("notification")
|
|
speaker = peripheral.find("speaker")
|
|
if not speaker then
|
|
print("Warning: No speaker attached.")
|
|
return false
|
|
end
|
|
return true
|
|
end
|
|
|
|
local function play(name)
|
|
if not speaker then return false end
|
|
|
|
local filePath
|
|
for _, sound in ipairs(sounds) do
|
|
if sound.name == name then
|
|
filePath = sound.file
|
|
break
|
|
end
|
|
end
|
|
|
|
if not filePath then
|
|
print("Sound '" .. name .. "' not defined.")
|
|
return false
|
|
end
|
|
|
|
if not fs.exists(filePath) then
|
|
print("File '" .. filePath .. "' not found.")
|
|
return false
|
|
end
|
|
|
|
-- Create a new decoder for this playback to reset state
|
|
local decoder = dfpwm.make_decoder()
|
|
|
|
for chunk in io.lines(filePath, 16 * 1024) do
|
|
local buffer = decoder(chunk)
|
|
while not speaker.playAudio(buffer) do
|
|
os.pullEvent("speaker_audio_empty")
|
|
end
|
|
end
|
|
return true
|
|
end
|
|
|
|
return { initialize = initialize, play = play }
|