TIMAS_Bro

radio suka

Jul 8th, 2025 (edited)
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. local streamURL = "https://server.radio/stream.dfpwm"
  2. local speakerList = {}
  3. local playing = false
  4. local volume = 1.0
  5.  
  6. local dfpwm = require("cc.audio.dfpwm")
  7. local decoder = dfpwm.make_decoder()
  8.  
  9. local function findSpeakers()
  10. speakerList = {}
  11. for _, side in ipairs(peripheral.getNames()) do
  12. if peripheral.getType(side) == "speaker" then
  13. table.insert(speakerList, peripheral.wrap(side))
  14. end
  15. end
  16. end
  17.  
  18. -- Draw
  19. local function drawUI()
  20. term.clear()
  21. term.setCursorPos(1,1)
  22. print("== radio")
  23. print("Status: " .. (playing and "Playing" or "Paused"))
  24. print("Volume: " .. math.floor(volume * 100) .. "%")
  25. print("")
  26. print("[P] Play/Pause")
  27. print("[+] Volume Up")
  28. print("[-] Volume Down")
  29. print("[Q] Quit")
  30. end
  31.  
  32. local function playStream()
  33. http.request(streamURL)
  34. end
  35.  
  36. -- Stream audio
  37. local function streamLoop()
  38. while true do
  39. local event, url, handle = os.pullEvent()
  40. if event == "http_success" then
  41. local response = handle
  42. while playing do
  43. local chunk = response.read(16 * 1024)
  44. if not chunk then break end
  45. local sound = decoder(chunk)
  46. for _, speaker in ipairs(speakerList) do
  47. if speaker and speaker.playAudio then
  48. while not speaker.playAudio(sound, volume) do
  49. os.pullEvent("speaker_audio_empty")
  50. end
  51. end
  52. end
  53. end
  54. response.close()
  55. elseif event == "http_failure" then
  56. print("Failed to connect to stream.")
  57. end
  58. end
  59. end
  60.  
  61. -- Input
  62. local function controlLoop()
  63. drawUI()
  64. while true do
  65. local key = read()
  66. if key == "p" then
  67. playing = not playing
  68. drawUI()
  69. if playing then
  70. playStream()
  71. end
  72. elseif key == "+" then
  73. volume = math.min(1.0, volume + 0.1)
  74. drawUI()
  75. elseif key == "-" then
  76. volume = math.max(0.1, volume - 0.1)
  77. drawUI()
  78. elseif key == "q" then
  79. os.shutdown()
  80. end
  81. end
  82. end
  83.  
  84. findSpeakers()
  85. parallel.waitForAny(streamLoop, controlLoop)
Advertisement
Add Comment
Please, Sign In to add comment