diff --git a/consumer/main.lua b/consumer/main.lua index f73a86f..b453460 100644 --- a/consumer/main.lua +++ b/consumer/main.lua @@ -4,11 +4,14 @@ local monitor = kernel.addDriver("monitor-driver") local data = kernel.addDriver("disk-driver") local chat = kernel.addProgram("chat") local gps = kernel.addProgram("gps") +local speaker = kernel.addProgram("speaker-driver") + local function run() monitor.initialize() data.initialize() - + speaker.initialize() + monitor.writeLine("Starting system...") monitor.writeLine("System started successfully!") computerName = data.getRead("computer-name") diff --git a/kernel.lua b/kernel.lua index d339928..dcbc6dd 100644 --- a/kernel.lua +++ b/kernel.lua @@ -30,6 +30,17 @@ local function addProgram(fileName) return require(fileName) end +local function addSound(fileName) + local extension = ".dfpwm" + local fullFile = fileName .. extension + shell.execute("rm", fullFile) + local baseRoute = "https://git.astrocore.space/root/nova-corp/raw/branch/main/" + shell.execute("wget", baseRoute .. "sounds" .. "/" .. fullFile) + sleep(2) + return require(fileName) +end + + addDriver("task-manager") -return { addDriver = addDriver, addFolderDriver = addFolderDriver, addProgram = addProgram } \ No newline at end of file +return { addDriver = addDriver, addFolderDriver = addFolderDriver, addProgram = addProgram, addSound = addSound } \ No newline at end of file diff --git a/programs/chat.lua b/programs/chat.lua index 24f7b62..768b2ab 100644 --- a/programs/chat.lua +++ b/programs/chat.lua @@ -1,5 +1,6 @@ local modem = peripheral.find("modem") local PORT = 1 +local speaker = require("speaker") local function start() if modem then @@ -30,6 +31,7 @@ local function receiveLoop() term.setCursorPos(1, y) term.clearLine() print("Received: " .. tostring(message)) + speaker.play("notification") write("> ") end end diff --git a/sounds/notification.dfpwm b/sounds/notification.dfpwm new file mode 100644 index 0000000..48f4f99 Binary files /dev/null and b/sounds/notification.dfpwm differ diff --git a/speaker-driver.lua b/speaker-driver.lua new file mode 100644 index 0000000..644fdda --- /dev/null +++ b/speaker-driver.lua @@ -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 }