Advertisement
MigasRocha

Audio Alarm ReactorCoolant Low /Turtle ID:46

Dec 26th, 2024
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.43 KB | Gaming | 0 0
  1. local dfpwmFile = fs.open("ReactorCoolant.dfpwm", "rb")
  2. if not dfpwmFile then
  3.     print("Failed to open file.")
  4.     return
  5. end
  6.  
  7. local speaker = peripheral.find("speaker")
  8. if not speaker then
  9.     print("No speaker found.")
  10.     dfpwmFile.close()
  11.     return
  12. end
  13.  
  14. local dfpwm = require("cc.audio.dfpwm").make_decoder()
  15. local chunkSize = 6 * 1024  -- 6 KB per chunk (approx)
  16. local sleepTime = 0.5  -- 0.5 second per chunk
  17.  
  18. local alarmActive = true  -- Used for manual override
  19.  
  20. -- Function to handle the manual override (stop the alarm)
  21. local function checkForOverride()
  22.     while true do
  23.         local event, key = os.pullEvent("key")
  24.         if key == keys.s then  -- Press 's' to toggle alarm
  25.             alarmActive = not alarmActive
  26.             if alarmActive then
  27.                 print("Alarm reactivated.")
  28.             else
  29.                 print("Alarm deactivated.")
  30.             end
  31.         end
  32.     end
  33. end
  34.  
  35. -- Play the audio queue if alarm is active
  36. local function playAudioQueue()
  37.     while alarmActive do
  38.         -- Reset file pointer to the beginning of the file
  39.         dfpwmFile.seek("set", 0)
  40.  
  41.         while alarmActive do
  42.             local chunk = dfpwmFile.read(chunkSize)
  43.             if not chunk then
  44.                 -- print("End of file reached, restarting.")  -- Commented out debug message
  45.                 break  -- Exit inner loop to restart the file from the beginning
  46.             end
  47.  
  48.             local decodedChunk = dfpwm(chunk)
  49.             if not decodedChunk then
  50.                 -- print("Error decoding chunk.")  -- Commented out debug message
  51.                 break
  52.             end
  53.  
  54.             -- Play the audio chunk
  55.             local success, err = pcall(function()
  56.                 speaker.playAudio(decodedChunk)
  57.             end)
  58.  
  59.             if not success then
  60.                 -- print("Error playing audio on speaker: " .. err)  -- Commented out debug message
  61.             else
  62.                 -- print("Playing audio chunk.")  -- Commented out debug message
  63.             end
  64.  
  65.             -- Sleep for the duration of each chunk
  66.             sleep(sleepTime)
  67.  
  68.         end
  69.     end
  70. end
  71.  
  72. -- Start the manual override and play audio queue
  73. parallel.waitForAll(checkForOverride, playAudioQueue)
  74.  
  75. dfpwmFile.close()
  76. -- print("Audio playback loop ended.")  -- Commented out debug message
  77.  
  78. --File Link --https://drive.google.com/file/d/1E4eNCRqd9li74hkIjvICLJMi9vmLzdTm/view?usp=drive_link--
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement