Advertisement
Marlingaming

CC Tweaked Music Player - V4

Jan 21st, 2022 (edited)
633
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. local Songs = {}
  2. local DisplayNames = {}
  3. local w, h = term.getSize()
  4. local PlayQueue = {}
  5. local MusicPath = ""
  6. local OSVersion
  7.  
  8. local function Clear()
  9. term.clear()
  10. term.setCursorPos(1,1)
  11. end
  12.  
  13. local function ListFiles()
  14. Songs = fs.list(MusicPath)
  15. DisplayNames = fs.list(MusicPath)
  16. for i = 1, #Songs do
  17. Songs[i] = fs.combine(MusicPath,Songs[i])
  18. end
  19. for i = 1, #Songs do
  20. local Len = string.len(DisplayNames[i])
  21. DisplayNames[i] = string.sub(DisplayNames[i],1,Len - 6)
  22. end
  23. for i = 1, 15 do
  24. PlayQueue[i] = math.random(1,#DisplayNames)
  25. end
  26. end
  27.  
  28.  
  29. function CUI(m,y) --declare function
  30. n=1
  31. local l = #m
  32. while true do
  33. term.setCursorPos(1,y)
  34. Clear()
  35. for i=1, #m, 1 do --traverse the table of options
  36. if i==n then term.clearLine() print(i, " >",m[i]) else term.clearLine() print(i, " ", m[i]) end --print them
  37. end
  38. a, b= os.pullEvent("key") --wait for keypress
  39. if b==keys.w and n>1 then n=n-1 end
  40. if b==keys.s and n<=l then n=n+1 end
  41. if b==keys.enter then break end
  42. end
  43. return n --return the value
  44. end
  45.  
  46. function SongPlayer(I)
  47. Clear()
  48. local dfpwm = require "cc.audio.dfpwm"
  49. local speaker = peripheral.find("speaker")
  50. local decoder = dfpwm.make_decoder()
  51. local BarChange = 0.2
  52. local BarSize = 1
  53. print("Now Playing "..DisplayNames[I])
  54. print("Length = ?")
  55. local event, key
  56. for input in io.lines(Songs[I], 16 * 1024) do
  57. local decoded = decoder(input)
  58. while not speaker.playAudio(decoded) do
  59. paintutils.drawFilledBox(7,6,BarSize,7,colors.red)
  60. BarSize = BarSize + BarChange
  61. os.pullEvent("speaker_audio_empty")
  62. end
  63. end
  64. io.close()
  65. MainMenu()
  66. end
  67.  
  68. function MainMenu()
  69. term.setBackgroundColor(colors.lightGray)
  70. Clear()
  71. ListFiles()
  72. local options = DisplayNames
  73. options[#options + 1] = "Playlist"
  74. options[#options + 1] = "random"
  75. options[#options + 1] = "exit"
  76. local n = CUI(options,2)
  77. if options[n] == "exit" then
  78. if OSVersion == "CCSPS - Iron" then
  79. shell.run(settings.get("os_DesktopLoc"))
  80. end
  81. elseif options[n] == "Playlist" then
  82. for i = 1, #PlayQueue do
  83. SongPlayer(PlayQueue[i])
  84. end
  85. elseif options[n] == "random" then
  86. local T = math.random(1,#DisplayNames)
  87. SongPlayer(T)
  88. else
  89. SongPlayer(n)
  90. end
  91. end
  92.  
  93. settings.load(".settings")
  94. OSVersion = settings.get("os_Version")
  95. if OSVersion == "CCSPS - Iron" then
  96. MusicPath = "os/System/Saved/Audio"
  97. else
  98. if fs.exists("os/os_SystemFiles/Audio") then
  99. MusicPath = "os/os_SystemFiles/Audio"
  100. else
  101. MusicPath = ""
  102. end
  103. end
  104.  
  105. ListFiles()
  106. if #DisplayNames > 0 then
  107. MainMenu()
  108. else
  109. print("no Songs installed")
  110. os.sleep(5)
  111. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement