Advertisement
MrFinn

ComputerCraft - Glados sound test

Nov 10th, 2024 (edited)
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. local dfpwm = require("cc.audio.dfpwm")
  2. local CHUNK_SIZE = 16 * 1024
  3. local unpack = unpack or table.unpack
  4. --- Play DFPWM audio from a file or URL
  5. ---@param path string Path (or direct URL) of the file to play
  6. ---@param speakers? table (Optional) List of speaker peripherals to play audio to
  7. local function play_dfpwm(path, speakers)
  8. assert(type(path) == "string", "bad argument #1 (string expected, got " .. type(path) .. ")")
  9. assert(#path > 0, "path argument is empty")
  10. speakers = speakers or { peripheral.find("speaker") }
  11. assert(#speakers > 0, "speaker peripheral is missing")
  12. local decoder = dfpwm.make_decoder()
  13. local function playBuffer(buffer)
  14. local players = {}
  15. for i, speaker in next, speakers do
  16. players[i] = function()
  17. while not speaker.playAudio(buffer) do
  18. os.pullEvent("speaker_audio_empty")
  19. end
  20. end
  21. end
  22. parallel.waitForAll(unpack(players))
  23. end
  24. if path:lower():match("^https?:") then
  25. assert(http and http.get, "http extension is disabled")
  26. local request = assert(http.get(path, nil, true))
  27. path = assert(request.readAll(), "failed to read response")
  28. request.close()
  29. for i = 1, #path, CHUNK_SIZE do
  30. playBuffer(decoder(path:sub(i, i + CHUNK_SIZE - 1)))
  31. end
  32. else
  33. assert(fs.exists(path), "file path does not exist")
  34. for chunk in io.lines(path, CHUNK_SIZE) do
  35. playBuffer(decoder(chunk))
  36. end
  37. end
  38. end
  39.  
  40. local speaker = peripheral.wrap("right")
  41.  
  42. play_dfpwm("https://www.lordfinn.fr/audios/4d2365d4-fbe1-44d7-a0ec-d70c7685d106_glados.dfpwm", {speaker})
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement