Added notificaiton sound
This commit is contained in:
56
speaker-driver.lua
Normal file
56
speaker-driver.lua
Normal file
@@ -0,0 +1,56 @@
|
||||
local speaker = peripheral.find("speaker")
|
||||
local dfpwm = require("cc.audio.dfpwm")
|
||||
local kernel = require("kernel")
|
||||
|
||||
|
||||
|
||||
local sounds = {
|
||||
{name = "notification", file = "notification.dfpwm"}
|
||||
}
|
||||
|
||||
local function initialize()
|
||||
|
||||
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 }
|
||||
Reference in New Issue
Block a user