Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local width, height = term.getSize()
- local Screen = "Main"
- local Selection = 0
- local Playing = nil
- local SongData
- local dfpwm = require("cc.audio.dfpwm")
- local decoder = dfpwm.make_decoder()
- local speaker = peripheral.find("speaker")
- function WriteCenter(thing)
- local x, y = term.getCursorPos()
- term.setCursorPos(math.ceil((width/2) - (string.len(thing)/2)) + 1, y)
- term.write(thing)
- end
- function PlaySong(song)
- local content = http.get(song, nil, true).readAll()
- if not content then
- return false
- end
- SongData = {}
- local pointer = 1
- for i = 1, string.len(content), 16 * 1024 do
- table.insert(SongData, string.sub(content, pointer, i))
- pointer = i+1
- end
- return true
- end
- fs.makeDir("songs")
- function UpdateScreen()
- term.clear()
- paintutils.drawLine(1, 1, width, 1, colors.red)
- WriteCenter("Ender MP3 Player")
- term.setCursorPos(1, 3)
- term.setBackgroundColor(colors.black)
- if Screen == "Main" then
- WriteCenter("[Options]")
- term.setCursorPos(1, 4)
- if Selection == 0 then
- WriteCenter("<Play Music>")
- else
- WriteCenter("Play Music")
- end
- term.setCursorPos(1, 5)
- if Selection == 1 then
- WriteCenter("<Download Music>")
- else
- WriteCenter("Download Music")
- end
- elseif Screen == "PlaySongs" then
- WriteCenter("[Play Songs]")
- local songs = fs.list("songs")
- for a,b in pairs(songs) do
- term.setCursorPos(0, 4+(a-1))
- if Selection == (a-1) then
- WriteCenter("<" .. b .. ">")
- else
- WriteCenter(b)
- end
- end
- term.setCursorPos(0, 4+#songs)
- if Selection == #songs then
- WriteCenter("<Back>")
- else
- WriteCenter("Back")
- end
- elseif Screen == "Song" then
- WriteCenter("[" .. Playing .. "]")
- term.setCursorPos(0, 4)
- WriteCenter("Song may take 5-10")
- term.setCursorPos(0, 5)
- WriteCenter("seconds to load.")
- term.setCursorPos(0, 6)
- WriteCenter("Press [Enter] to stop...")
- term.setCursorPos(0, 8)
- end
- end
- function Main()
- while true do
- local event, key, isHeld = os.pullEvent("key")
- --print(keys.getName( key ))
- if key == keys.up or key == keys.down then
- if key == keys.up then
- Selection = Selection - 1
- else
- Selection = Selection + 1
- end
- if Screen == "Main" then
- if Selection > 1 then
- Selection = 0
- elseif Selection < 0 then
- Selection = 1
- end
- elseif Screen == "PlaySongs" then
- if Selection > #fs.list("songs") then
- Selection = 0
- elseif Selection < 0 then
- Selection = #fs.list("songs")
- end
- else
- Selection = 0
- end
- elseif key == keys.enter then
- if Screen == "Main" then
- if Selection == 0 then
- Screen = "PlaySongs"
- Selection = 0
- end
- elseif Screen == "PlaySongs" then
- local songs = fs.list("songs")
- if Selection == #songs then
- Screen = "Main"
- Selection = 0
- else
- local songname = songs[Selection+1]
- local song = fs.open("songs/" .. songname, "r").readLine()
- if PlaySong(song) then
- Playing = songname
- else
- print("failed to load")
- end
- Screen = "Song"
- Selection = 0
- end
- elseif Screen == "Song" then
- if Selection == 0 then
- Playing = nil
- Screen = "PlaySongs"
- Selection = 0
- end
- end
- end
- UpdateScreen()
- end
- end
- parallel.waitForAny(Main, function()
- while true do
- if Playing ~= nil then
- for num,chunk in pairs(SongData) do
- UpdateScreen()
- WriteCenter("Chunk #" .. num .. "/" .. #SongData)
- local buffer = decoder(chunk)
- while not speaker.playAudio(buffer) do
- os.pullEvent("speaker_audio_empty")
- end
- if Playing == nil then
- break
- end
- end
- else
- sleep(0.1)
- end
- end
- end)
Advertisement
Comments
-
- hello, how can we add song ?
-
- I'll eventually make it so you can download songs for this, but this is not finished and is only experimental.
Add Comment
Please, Sign In to add comment
Advertisement