Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- playlistUrl = "ZombiGik/Songs"
- local playlist = {}
- local songs = {}
- songs.scroll = 0
- local stop = false
- local currentSong = nil
- local dfpwm = require("cc.audio.dfpwm")
- local monitor = peripheral.find("monitor")
- local _W, _H = monitor.getSize()
- local speaker = peripheral.find("speaker")
- local decoder = dfpwm.make_decoder()
- songs.window = window.create(monitor,1,1,_W-1,_H-1)
- songs.window.setBackgroundColor(colors.gray)
- songs.window.setTextColor(colors.lime)
- songs.window.clear()
- songs.scrollbar = window.create(monitor,_W,1,1,_H-1)
- songs.scrollbar.setBackgroundColor(colors.blue)
- songs.scrollbar.setTextColor(colors.lime)
- songs.scrollbar.clear()
- function updatePlaylist()
- songs.names = {}
- content = textutils.unserialiseJSON(http.get("https://api.github.com/repos/"..playlistUrl.."/contents/").readAll())
- for _,song in pairs(content) do
- playlist[song.name] = {}
- playlist[song.name].url = song.download_url
- table.insert(songs.names,song.name)
- end
- table.sort(songs.names)
- end
- function redraw()
- monitor.clear()
- songs.window.clear()
- songs.scrollbar.clear()
- songs.scrollbar.setCursorPos(1,1)
- songs.scrollbar.write(string.char(24))
- songs.scrollbar.setCursorPos(1,_H-1)
- songs.scrollbar.write(string.char(25))
- for i, title in ipairs(songs.names) do
- songs.window.setCursorPos(1,i+songs.scroll)
- songs.window.write(title)
- end
- end
- function mainscreen()
- event, _, posX, posY= os.pullEvent("monitor_touch")
- if posX <= _W-1 and posY <= _H-1 then
- title,fg,bg = songs.window.getLine(posY)
- titleStart, titleEnd = string.find(title,"(.+)%S")
- if titleStart ~= nil then
- if playlist[string.sub(title,titleStart,titleEnd)].data == nil then
- print("Downloading...")
- local response = http.get(playlist[string.sub(title,titleStart,titleEnd)].url,nil,true)
- playlist[string.sub(title,titleStart,titleEnd)].data = response.readAll()
- response.close()
- print("Downloaded!")
- else
- print("Exist!")
- end
- currentSong = string.sub(title,titleStart,titleEnd)
- end
- elseif posY <= _H-1 then
- if posY < (_H-1)/2 then
- songs.scroll = songs.scroll + 1
- else
- songs.scroll = songs.scroll - 1
- end
- else
- stop = true
- updatePlaylist()
- end
- redraw()
- end
- function play()
- print(currentSong)
- if currentSong ~= nil then
- for i = 1, #playlist[currentSong].data, 16*64 do
- local buffer = decoder(playlist[currentSong].data:sub(i,i+16*64-1))
- if stop then
- stop = false
- currentSong = nil
- return
- end
- while not speaker.playAudio(buffer) do
- os.pullEvent("speaker_audio_empty")
- end
- end
- currentSong = nil
- else
- sleep(3000)
- end
- end
- updatePlaylist()
- redraw()
- while true do
- parallel.waitForAny(mainscreen,play)
- end
Advertisement
Add Comment
Please, Sign In to add comment