ZombieGeek

Music

Apr 28th, 2022 (edited)
1,076
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. playlistUrl = "ZombiGik/Songs"
  2.  
  3.  
  4. local playlist = {}
  5. local songs = {}
  6. songs.scroll = 0
  7. local stop = false
  8. local currentSong = nil
  9. local dfpwm = require("cc.audio.dfpwm")
  10. local monitor = peripheral.find("monitor")
  11. local _W, _H = monitor.getSize()
  12. local speaker = peripheral.find("speaker")
  13. local decoder = dfpwm.make_decoder()
  14.  
  15. songs.window = window.create(monitor,1,1,_W-1,_H-1)
  16. songs.window.setBackgroundColor(colors.gray)
  17. songs.window.setTextColor(colors.lime)
  18. songs.window.clear()
  19.  
  20. songs.scrollbar = window.create(monitor,_W,1,1,_H-1)
  21. songs.scrollbar.setBackgroundColor(colors.blue)
  22. songs.scrollbar.setTextColor(colors.lime)
  23. songs.scrollbar.clear()
  24.  
  25. function updatePlaylist()
  26.     songs.names = {}
  27.     content = textutils.unserialiseJSON(http.get("https://api.github.com/repos/"..playlistUrl.."/contents/").readAll())
  28.     for _,song in pairs(content) do
  29.         playlist[song.name] = {}
  30.         playlist[song.name].url = song.download_url
  31.         table.insert(songs.names,song.name)
  32.     end
  33.     table.sort(songs.names)
  34. end
  35.  
  36. function redraw()
  37.     monitor.clear()
  38.     songs.window.clear()
  39.     songs.scrollbar.clear()
  40.     songs.scrollbar.setCursorPos(1,1)
  41.     songs.scrollbar.write(string.char(24))
  42.     songs.scrollbar.setCursorPos(1,_H-1)
  43.     songs.scrollbar.write(string.char(25))
  44.    
  45.     for i, title in ipairs(songs.names) do
  46.         songs.window.setCursorPos(1,i+songs.scroll)
  47.         songs.window.write(title)
  48.     end
  49. end
  50.  
  51. function mainscreen()
  52.     event, _, posX, posY= os.pullEvent("monitor_touch")
  53.     if posX <= _W-1 and posY <= _H-1 then
  54.         title,fg,bg = songs.window.getLine(posY)
  55.         titleStart, titleEnd = string.find(title,"(.+)%S")
  56.         if titleStart ~= nil then
  57.             if playlist[string.sub(title,titleStart,titleEnd)].data == nil then
  58.                 print("Downloading...")
  59.                 local response = http.get(playlist[string.sub(title,titleStart,titleEnd)].url,nil,true)
  60.                 playlist[string.sub(title,titleStart,titleEnd)].data = response.readAll()
  61.                 response.close()
  62.                 print("Downloaded!")
  63.             else
  64.                 print("Exist!")
  65.             end
  66.             currentSong = string.sub(title,titleStart,titleEnd)
  67.         end
  68.     elseif posY <= _H-1 then
  69.         if posY < (_H-1)/2 then
  70.             songs.scroll = songs.scroll + 1
  71.         else
  72.             songs.scroll = songs.scroll - 1
  73.         end
  74.     else
  75.         stop = true
  76.         updatePlaylist()
  77.     end
  78.     redraw()
  79. end
  80.  
  81. function play()
  82.     print(currentSong)
  83.     if currentSong ~= nil then
  84.         for i = 1, #playlist[currentSong].data, 16*64 do
  85.         local buffer = decoder(playlist[currentSong].data:sub(i,i+16*64-1))
  86.             if stop then
  87.                 stop = false
  88.                 currentSong = nil
  89.                 return
  90.             end
  91.             while not speaker.playAudio(buffer) do
  92.                 os.pullEvent("speaker_audio_empty")
  93.             end
  94.         end
  95.         currentSong = nil
  96.     else
  97.         sleep(3000)
  98.     end
  99. end
  100.  
  101. updatePlaylist()
  102. redraw()
  103.  
  104. while true do
  105.     parallel.waitForAny(mainscreen,play)
  106. end
Advertisement
Add Comment
Please, Sign In to add comment