Advertisement
Z1maV1

audioPlayer

May 7th, 2024 (edited)
561
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.96 KB | None | 0 0
  1. local dfpwm = require("cc.audio.dfpwm")
  2. local linc = require("/lib/linc")
  3. local speaker = peripheral.find("speaker")
  4.  
  5. local function merge(t1, t2)
  6.     for i=1, #t2 do
  7.         t1[#t1+1] = t2[i]
  8.     end
  9.     return t1
  10. end
  11.  
  12. local audios = fs.find("/*.dfpwm")
  13. audios = merge(audios, fs.find("/*/*.dfpwm"))
  14. audios = merge(audios, fs.find("/*/*/*.dfpwm"))
  15. audios = merge(audios, fs.find("/*/*/*/*.dfpwm"))
  16.  
  17. local w, h = term.getSize()
  18. local nOption = 1
  19. local isPlayingAudio = false
  20. local audioLength = 0
  21. local currentChunk = 0
  22. local isRepeating = false
  23. local audio;
  24. local title = "TuneBox"
  25.  
  26. local function drawMusicSelector()
  27.    
  28.     linc.drawTopBar(title)
  29.     print()
  30.    
  31.     for i, v in ipairs(audios) do
  32.         if nOption == i then
  33.             term.setBackgroundColor(colors.lightGray)
  34.             term.setTextColor(colors.black)
  35.            
  36.             term.clearLine()
  37.             print(" > "..v.." ("..tostring(math.floor(fs.getSize(v) / 1024)).." KB)")
  38.            
  39.             term.setBackgroundColor(colors.black)
  40.             term.setTextColor(colors.white)
  41.         else
  42.             print("   "..v.." ("..tostring(math.floor(fs.getSize(v) / 1024)).." KB)")
  43.         end
  44.     end
  45. end
  46.  
  47. local function drawButton(str, bgcolor, fgcolor, keycolor)
  48.     local bg = term.getBackgroundColor()
  49.     local fg = term.getTextColor()
  50.     write(" ")
  51.     term.setBackgroundColor(bgcolor)
  52.     term.setTextColor(fgcolor)
  53.     term.write("[ ")
  54.  
  55.     term.setTextColor(keycolor)
  56.     term.write(str:sub(1,1))
  57.     term.setTextColor(fgcolor)
  58.     term.write(str:sub(2, #str) .. " ]")
  59.     term.setBackgroundColor(bg)
  60.     term.setTextColor(fg)
  61.     write(" ")
  62. end
  63.  
  64. local function lerp(a, b, t)
  65.     return a + (b - a) * t
  66. end
  67.  
  68. local function drawPlayer()
  69.  
  70.     linc.drawTopBar(title)
  71.     print()
  72.    
  73.     print(" Now playing: \""..audio:sub(1,#audio - 6).."\"\n")
  74.  
  75.     write(" ")
  76.     for i = 1, w - 2 do
  77.         if i < math.floor(lerp(0, w - 2, currentChunk / audioLength)) then
  78.             term.setTextColor(colors.green)
  79.             write("|")
  80.             term.setTextColor(colors.white)
  81.         else
  82.             term.setTextColor(colors.white)
  83.             write("|")
  84.         end
  85.     end
  86.  
  87.     print("\n")
  88.  
  89.     linc.drawButton("Prev", colors.gray, colors.lightGray, colors.white)
  90.     linc.drawButton("Stop", colors.gray, colors.lightGray, colors.white)
  91.     linc.drawButton("Next", colors.gray, colors.lightGray, colors.white)
  92.     if not isRepeating then
  93.         linc.drawButton("Repeat", colors.gray, colors.lightGray, colors.white)
  94.     else
  95.         --term.setTextColor(colors.black)
  96.         linc.drawButton("Repeat", colors.green, colors.gray, colors.white)
  97.         --term.setTextColor(colors.white)
  98.     end
  99. end
  100.  
  101. local function playAudioSchedule()
  102.     local decoder = dfpwm.make_decoder()
  103.  
  104.     while true do
  105.         if isPlayingAudio then
  106.             decoder = dfpwm.make_decoder()
  107.             audioLength = 0
  108.             for _ in io.lines(audios[nOption], 16 * 1024) do
  109.                 audioLength = audioLength + 1
  110.             end
  111.             currentChunk = 1
  112.             for chunk in io.lines(audios[nOption], 16 * 1024) do
  113.                 local buffer = decoder(chunk)
  114.  
  115.                 if not isPlayingAudio then
  116.                     break
  117.                 end
  118.        
  119.                 while not speaker.playAudio(buffer) do
  120.                     os.pullEvent("speaker_audio_empty")
  121.                 end
  122.                 currentChunk = currentChunk + 1
  123.                 drawPlayer()
  124.             end
  125.             if not isRepeating then
  126.                 sPlayingAudio = false
  127.             end
  128.         end
  129.         sleep(0)
  130.     end
  131. end
  132.    
  133.  
  134. local function handleInput()
  135.     while true do
  136.         local event, key = os.pullEvent("key")
  137.         if event == "key" then
  138.             if not isPlayingAudio then
  139.                 if keys.getName(key) == "up" then
  140.                     if nOption > 1 then
  141.                         nOption = nOption - 1
  142.                     end
  143.                 elseif keys.getName(key) == "down" then
  144.                     if nOption < #audios then
  145.                         nOption = nOption + 1
  146.                     end
  147.                 elseif keys.getName(key) == "enter" then
  148.                     isPlayingAudio = true
  149.                     audio = fs.getName(audios[nOption])
  150.                     goto continue
  151.                 end
  152.  
  153.                 drawMusicSelector()
  154.             else
  155.                 if keys.getName(key) == "r" then
  156.                     isRepeating = not isRepeating
  157.                     drawPlayer()
  158.                 elseif keys.getName(key) == "p" then
  159.                     if nOption > 1 then
  160.                         nOption = nOption - 1
  161.  
  162.                         audio = fs.getName(audios[nOption])
  163.                         isPlayingAudio = false
  164.  
  165.                         sleep(3)
  166.  
  167.                         isPlayingAudio = true
  168.                     end
  169.                 elseif keys.getName(key) == "n" then
  170.                     if nOption < #audios then
  171.                         nOption = nOption + 1
  172.  
  173.                         audio = fs.getName(audios[nOption])
  174.                         isPlayingAudio = false
  175.  
  176.                         sleep(3)
  177.  
  178.                         isPlayingAudio = true
  179.                     end
  180.                 elseif keys.getName(key) == "s" then
  181.                     isPlayingAudio = false
  182.                     sleep(3)
  183.                     drawMusicSelector()
  184.                 end
  185.             end
  186.         end
  187.  
  188.  
  189.         ::continue::
  190.     end
  191. end
  192.  
  193. local function handleExit()
  194.     while true do
  195.         local event = os.pullEventRaw("terminate")
  196.         if event == "terminate" then
  197.             term.setBackgroundColor(colors.black)
  198.             term.setTextColor(colors.white)
  199.             term.clear()
  200.             term.setCursorPos(1,1)
  201.             break
  202.         end
  203.     end
  204. end
  205.    
  206.  
  207. if speaker == nil then
  208.     error("Cannot find speaker!")
  209. end
  210.  
  211. drawMusicSelector()
  212. parallel.waitForAny(handleInput, playAudioSchedule, handleExit)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement