Advertisement
Snowdingerr

CC:Tweaked Music Player

Feb 16th, 2025
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.53 KB | Software | 0 0
  1. -- music_player
  2. -- use arrows to move
  3. -- in the menus, and enter or space for clicks or pause
  4. -- works with .dfpwm musics in the root folder
  5.  
  6. local dfpwm = require("cc.audio.dfpwm")
  7.  
  8. -- Local variables
  9. local screenWidth, screenHeight = term.getSize()
  10. local selectedIndex = 1
  11. local speakers = { peripheral.find("speaker") }
  12. if #speakers == 0 then
  13.     error("No speakers attached", 0)
  14. end
  15. local speaker = speakers[1]
  16.  
  17. function drawMenu(songs)
  18.     term.setBackgroundColor(colors.black)
  19.     term.clear()
  20.     term.setTextColor(colors.white)
  21.     term.setBackgroundColor(colors.green)
  22.     term.setCursorPos(1, 1)
  23.     term.write("       Music Player       ")
  24.     term.setBackgroundColor(colors.black)
  25.     term.setTextColor(colors.white)
  26.     for i, song in ipairs(songs) do
  27.         if i == selectedIndex then
  28.             term.setBackgroundColor(colors.gray)
  29.         else
  30.             term.setBackgroundColor(colors.black)
  31.         end
  32.         term.setCursorPos(1, i + 1)
  33.         term.write("|> " .. string.gsub(song, ".dfpwm", ""))
  34.     end
  35. end
  36.  
  37. function displayPlayingIcon()
  38.     local icon = {
  39.         "   ",
  40.         "   ",
  41.         "   ",
  42.         "   ",
  43.         "   ",
  44.         "   "
  45.     }
  46.     local iconX = math.floor((screenWidth - #icon[1]) / 2) - 2
  47.     local iconY = math.floor((screenHeight - #icon) / 2)
  48.     term.setBackgroundColor(colors.black)
  49.     term.clear()
  50.     term.setTextColor(colors.white)
  51.     term.setBackgroundColor(colors.white)
  52.     for i, line in ipairs(icon) do
  53.         term.setCursorPos(iconX, iconY + i)
  54.         term.write(line)
  55.        
  56.         term.setCursorPos(iconX + 6, iconY + i)
  57.         term.write(line)
  58.     end
  59.     term.setBackgroundColor(colors.black)
  60. end
  61.  
  62. function playSong(song)
  63.     local path = song
  64.     local file = io.open(path, "rb")
  65.     if not file then
  66.         print("Failed to open file: " .. song)
  67.         return false
  68.     end
  69.  
  70.     displayPlayingIcon()
  71.  
  72.     local decoder = dfpwm.make_decoder()
  73.     for chunk in file:lines(16 * 1024) do
  74.         local buffer = decoder(chunk)
  75.  
  76.         while not speaker.playAudio(buffer) do
  77.             os.pullEvent("speaker_audio_empty")
  78.         end
  79.     end
  80.  
  81.     file:close()
  82.     return true
  83. end
  84.  
  85. function listSongs()
  86.     local path = "/"
  87.     if path then
  88.         local files = fs.list(path)
  89.         local songs = {}
  90.         for _, file in ipairs(files) do
  91.             if file:sub(-6) == ".dfpwm" then
  92.                 table.insert(songs, file)
  93.             end
  94.         end
  95.         return songs
  96.     else
  97.         print("No files found")
  98.         return {}
  99.     end
  100. end
  101.  
  102. function main()
  103.     local songs = listSongs()
  104.     drawMenu(songs)
  105.  
  106.     while true do
  107.         local event, key = os.pullEvent("key")
  108.  
  109.         if key == keys.up then
  110.             selectedIndex = selectedIndex - 1
  111.             if selectedIndex < 1 then
  112.                 selectedIndex = #songs
  113.             end
  114.         elseif key == keys.down then
  115.             selectedIndex = selectedIndex + 1
  116.             if selectedIndex > #songs then
  117.                 selectedIndex = 1
  118.             end
  119.         elseif key == keys.enter then
  120.             local selectedSong = songs[selectedIndex]
  121.             parallel.waitForAny(
  122.                 function()
  123.                     if playSong(selectedSong) then
  124.                         os.reboot()
  125.                     end
  126.                 end,
  127.                 function()
  128.                     os.pullEvent("key")
  129.                     os.reboot()
  130.                 end
  131.             )
  132.         end
  133.  
  134.         drawMenu(songs)
  135.     end
  136. end
  137.  
  138. main()
  139.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement