Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- music_player
- -- use arrows to move
- -- in the menus, and enter or space for clicks or pause
- -- works with .dfpwm musics in the root folder
- local dfpwm = require("cc.audio.dfpwm")
- -- Local variables
- local screenWidth, screenHeight = term.getSize()
- local selectedIndex = 1
- local speakers = { peripheral.find("speaker") }
- if #speakers == 0 then
- error("No speakers attached", 0)
- end
- local speaker = speakers[1]
- function drawMenu(songs)
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.green)
- term.setCursorPos(1, 1)
- term.write(" Music Player ")
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- for i, song in ipairs(songs) do
- if i == selectedIndex then
- term.setBackgroundColor(colors.gray)
- else
- term.setBackgroundColor(colors.black)
- end
- term.setCursorPos(1, i + 1)
- term.write("|> " .. string.gsub(song, ".dfpwm", ""))
- end
- end
- function displayPlayingIcon()
- local icon = {
- " ",
- " ",
- " ",
- " ",
- " ",
- " "
- }
- local iconX = math.floor((screenWidth - #icon[1]) / 2) - 2
- local iconY = math.floor((screenHeight - #icon) / 2)
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.white)
- for i, line in ipairs(icon) do
- term.setCursorPos(iconX, iconY + i)
- term.write(line)
- term.setCursorPos(iconX + 6, iconY + i)
- term.write(line)
- end
- term.setBackgroundColor(colors.black)
- end
- function playSong(song)
- local path = song
- local file = io.open(path, "rb")
- if not file then
- print("Failed to open file: " .. song)
- return false
- end
- displayPlayingIcon()
- local decoder = dfpwm.make_decoder()
- for chunk in file:lines(16 * 1024) do
- local buffer = decoder(chunk)
- while not speaker.playAudio(buffer) do
- os.pullEvent("speaker_audio_empty")
- end
- end
- file:close()
- return true
- end
- function listSongs()
- local path = "/"
- if path then
- local files = fs.list(path)
- local songs = {}
- for _, file in ipairs(files) do
- if file:sub(-6) == ".dfpwm" then
- table.insert(songs, file)
- end
- end
- return songs
- else
- print("No files found")
- return {}
- end
- end
- function main()
- local songs = listSongs()
- drawMenu(songs)
- while true do
- local event, key = os.pullEvent("key")
- if key == keys.up then
- selectedIndex = selectedIndex - 1
- if selectedIndex < 1 then
- selectedIndex = #songs
- end
- elseif key == keys.down then
- selectedIndex = selectedIndex + 1
- if selectedIndex > #songs then
- selectedIndex = 1
- end
- elseif key == keys.enter then
- local selectedSong = songs[selectedIndex]
- parallel.waitForAny(
- function()
- if playSong(selectedSong) then
- os.reboot()
- end
- end,
- function()
- os.pullEvent("key")
- os.reboot()
- end
- )
- end
- drawMenu(songs)
- end
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement