Advertisement
EnderReaper64

ComputerCraft MP3 Player (Pocket Computer)

Nov 22nd, 2022 (edited)
208
1
Never
2
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.82 KB | None | 1 0
  1. local width, height = term.getSize()
  2. local Screen = "Main"
  3. local Selection = 0
  4. local Playing = nil
  5. local SongData
  6. local dfpwm = require("cc.audio.dfpwm")
  7. local decoder = dfpwm.make_decoder()
  8. local speaker = peripheral.find("speaker")
  9.  
  10. function WriteCenter(thing)
  11. local x, y = term.getCursorPos()
  12. term.setCursorPos(math.ceil((width/2) - (string.len(thing)/2)) + 1, y)
  13. term.write(thing)
  14. end
  15.  
  16. function PlaySong(song)
  17. local content = http.get(song, nil, true).readAll()
  18. if not content then
  19. return false
  20. end
  21.  
  22. SongData = {}
  23. local pointer = 1
  24. for i = 1, string.len(content), 16 * 1024 do
  25. table.insert(SongData, string.sub(content, pointer, i))
  26. pointer = i+1
  27. end
  28. return true
  29. end
  30.  
  31. fs.makeDir("songs")
  32. function UpdateScreen()
  33. term.clear()
  34. paintutils.drawLine(1, 1, width, 1, colors.red)
  35. WriteCenter("Ender MP3 Player")
  36. term.setCursorPos(1, 3)
  37. term.setBackgroundColor(colors.black)
  38. if Screen == "Main" then
  39. WriteCenter("[Options]")
  40. term.setCursorPos(1, 4)
  41. if Selection == 0 then
  42. WriteCenter("<Play Music>")
  43. else
  44. WriteCenter("Play Music")
  45. end
  46. term.setCursorPos(1, 5)
  47. if Selection == 1 then
  48. WriteCenter("<Download Music>")
  49. else
  50. WriteCenter("Download Music")
  51. end
  52. elseif Screen == "PlaySongs" then
  53. WriteCenter("[Play Songs]")
  54. local songs = fs.list("songs")
  55. for a,b in pairs(songs) do
  56. term.setCursorPos(0, 4+(a-1))
  57. if Selection == (a-1) then
  58. WriteCenter("<" .. b .. ">")
  59. else
  60. WriteCenter(b)
  61. end
  62. end
  63. term.setCursorPos(0, 4+#songs)
  64. if Selection == #songs then
  65. WriteCenter("<Back>")
  66. else
  67. WriteCenter("Back")
  68. end
  69. elseif Screen == "Song" then
  70. WriteCenter("[" .. Playing .. "]")
  71. term.setCursorPos(0, 4)
  72. WriteCenter("Song may take 5-10")
  73. term.setCursorPos(0, 5)
  74. WriteCenter("seconds to load.")
  75. term.setCursorPos(0, 6)
  76. WriteCenter("Press [Enter] to stop...")
  77. term.setCursorPos(0, 8)
  78. end
  79. end
  80.  
  81. function Main()
  82. while true do
  83. local event, key, isHeld = os.pullEvent("key")
  84. --print(keys.getName( key ))
  85. if key == keys.up or key == keys.down then
  86. if key == keys.up then
  87. Selection = Selection - 1
  88. else
  89. Selection = Selection + 1
  90. end
  91. if Screen == "Main" then
  92. if Selection > 1 then
  93. Selection = 0
  94. elseif Selection < 0 then
  95. Selection = 1
  96. end
  97. elseif Screen == "PlaySongs" then
  98. if Selection > #fs.list("songs") then
  99. Selection = 0
  100. elseif Selection < 0 then
  101. Selection = #fs.list("songs")
  102. end
  103. else
  104. Selection = 0
  105. end
  106. elseif key == keys.enter then
  107. if Screen == "Main" then
  108. if Selection == 0 then
  109. Screen = "PlaySongs"
  110. Selection = 0
  111. end
  112. elseif Screen == "PlaySongs" then
  113. local songs = fs.list("songs")
  114. if Selection == #songs then
  115. Screen = "Main"
  116. Selection = 0
  117. else
  118. local songname = songs[Selection+1]
  119. local song = fs.open("songs/" .. songname, "r").readLine()
  120. if PlaySong(song) then
  121. Playing = songname
  122. else
  123. print("failed to load")
  124. end
  125. Screen = "Song"
  126. Selection = 0
  127. end
  128. elseif Screen == "Song" then
  129. if Selection == 0 then
  130. Playing = nil
  131. Screen = "PlaySongs"
  132. Selection = 0
  133. end
  134. end
  135. end
  136. UpdateScreen()
  137. end
  138. end
  139.  
  140. parallel.waitForAny(Main, function()
  141. while true do
  142. if Playing ~= nil then
  143. for num,chunk in pairs(SongData) do
  144. UpdateScreen()
  145. WriteCenter("Chunk #" .. num .. "/" .. #SongData)
  146. local buffer = decoder(chunk)
  147.  
  148. while not speaker.playAudio(buffer) do
  149. os.pullEvent("speaker_audio_empty")
  150. end
  151.  
  152. if Playing == nil then
  153. break
  154. end
  155. end
  156. else
  157. sleep(0.1)
  158. end
  159. end
  160. end)
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement