Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local dfpwmFile = fs.open("ReactorCoolant.dfpwm", "rb")
- if not dfpwmFile then
- print("Failed to open file.")
- return
- end
- local speaker = peripheral.find("speaker")
- if not speaker then
- print("No speaker found.")
- dfpwmFile.close()
- return
- end
- local dfpwm = require("cc.audio.dfpwm").make_decoder()
- local chunkSize = 6 * 1024 -- 6 KB per chunk (approx)
- local sleepTime = 0.5 -- 0.5 second per chunk
- local alarmActive = true -- Used for manual override
- -- Function to handle the manual override (stop the alarm)
- local function checkForOverride()
- while true do
- local event, key = os.pullEvent("key")
- if key == keys.s then -- Press 's' to toggle alarm
- alarmActive = not alarmActive
- if alarmActive then
- print("Alarm reactivated.")
- else
- print("Alarm deactivated.")
- end
- end
- end
- end
- -- Play the audio queue if alarm is active
- local function playAudioQueue()
- while alarmActive do
- -- Reset file pointer to the beginning of the file
- dfpwmFile.seek("set", 0)
- while alarmActive do
- local chunk = dfpwmFile.read(chunkSize)
- if not chunk then
- -- print("End of file reached, restarting.") -- Commented out debug message
- break -- Exit inner loop to restart the file from the beginning
- end
- local decodedChunk = dfpwm(chunk)
- if not decodedChunk then
- -- print("Error decoding chunk.") -- Commented out debug message
- break
- end
- -- Play the audio chunk
- local success, err = pcall(function()
- speaker.playAudio(decodedChunk)
- end)
- if not success then
- -- print("Error playing audio on speaker: " .. err) -- Commented out debug message
- else
- -- print("Playing audio chunk.") -- Commented out debug message
- end
- -- Sleep for the duration of each chunk
- sleep(sleepTime)
- end
- end
- end
- -- Start the manual override and play audio queue
- parallel.waitForAll(checkForOverride, playAudioQueue)
- dfpwmFile.close()
- -- print("Audio playback loop ended.") -- Commented out debug message
- --File Link --https://drive.google.com/file/d/1E4eNCRqd9li74hkIjvICLJMi9vmLzdTm/view?usp=drive_link--
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement