Advertisement
colhaydutu

new music player

Jan 10th, 2024 (edited)
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.34 KB | None | 0 0
  1. local speaker = peripheral.find("speaker")
  2. local dfpwm = require("cc.audio.dfpwm")
  3. local monitor = peripheral.wrap("left")
  4. local songLoaded = false -- Şarkının yüklenip yüklenmediğini kontrol etmek için bayrak
  5. local modemSide = "back"
  6. local isPlaying = false
  7. local volume = 1.0
  8. local maxBarWidth = 16 -- Maksimum çubuk genişliği
  9.  
  10. local function drawControls()
  11. monitor.setBackgroundColor(colors.gray)
  12. monitor.clear()
  13. term.redirect(monitor)
  14. monitor.setCursorPos(6, 5)
  15. monitor.setTextScale(1.5)
  16. monitor.setTextColor(colors.white)
  17.  
  18. if isPlaying then
  19. paintutils.drawLine(5, 6, 15, 6, colors.red)
  20. paintutils.drawLine(5, 4, 15, 4, colors.red)
  21. monitor.setCursorPos(5, 5)
  22. monitor.setBackgroundColor(colors.red)
  23. monitor.setTextColor(colors.white)
  24. monitor.write(" [Stop ||] ")
  25. else
  26. paintutils.drawLine(5, 6, 15, 6, colors.green)
  27. paintutils.drawLine(5, 4, 15, 4, colors.green)
  28. monitor.setCursorPos(5, 5)
  29. monitor.setBackgroundColor(colors.green)
  30. monitor.setTextColor(colors.white)
  31. monitor.write(" [Play |>] ")
  32. end
  33.  
  34. paintutils.drawLine(4, 8, 16, 8, colors.blue)
  35. paintutils.drawLine(4, 10, 16, 10, colors.blue)
  36. monitor.setCursorPos(4, 9)
  37. monitor.setBackgroundColor(colors.blue)
  38. monitor.setTextColor(colors.white)
  39. monitor.write(" [Rewind <<] ")
  40.  
  41. paintutils.drawLine(4, 17, 15, 17, colors.orange)
  42. paintutils.drawLine(4, 15, 15, 15, colors.orange)
  43. monitor.setCursorPos(4, 16)
  44. monitor.setBackgroundColor(colors.orange)
  45. monitor.setTextColor(colors.black)
  46. monitor.write(" [ Upload ] ")
  47.  
  48. -- Draw volume bar
  49. monitor.setCursorPos(3, 12)
  50. monitor.setBackgroundColor(colors.gray)
  51. monitor.setTextColor(colors.white)
  52. monitor.write("[Volume: " .. tostring(volume * 100) .."%]")
  53.  
  54. monitor.setCursorPos(2, 14)
  55. local barWidth = math.floor(volume * maxBarWidth)
  56. local emptyWidth = maxBarWidth - barWidth
  57. for i = 1, barWidth do
  58. monitor.setBackgroundColor(colors.green)
  59. monitor.write(" ")
  60. end
  61. for i = 1, emptyWidth do
  62. monitor.setBackgroundColor(colors.gray)
  63. monitor.write(" ")
  64. end
  65.  
  66. monitor.setCursorPos(1, 14)
  67. monitor.setBackgroundColor(colors.gray)
  68. monitor.setTextColor(colors.white)
  69. monitor.write("[ ")
  70.  
  71. monitor.setCursorPos(17, 14)
  72. monitor.setBackgroundColor(colors.gray)
  73. monitor.setTextColor(colors.white)
  74. monitor.write(" ]")
  75.  
  76. monitor.setCursorPos(2, 12)
  77. monitor.setBackgroundColor(colors.blue)
  78. monitor.setTextColor(colors.white)
  79. monitor.write("<")
  80.  
  81. monitor.setCursorPos(17, 12)
  82. monitor.setBackgroundColor(colors.blue)
  83. monitor.setTextColor(colors.white)
  84. monitor.write(">")
  85.  
  86. local w, h = monitor.getSize()
  87.  
  88. monitor.setCursorPos(1, 1)
  89. monitor.setBackgroundColor(colors.green)
  90. monitor.setTextColor(colors.white)
  91. monitor.write(" Now playing ")
  92. monitor.setCursorPos(1, 2)
  93. monitor.write("tapeLabel")
  94.  
  95. if lamba then
  96. monitor.setCursorPos(17, 17)
  97. monitor.setBackgroundColor(colors.green)
  98. monitor.setTextColor(colors.white)
  99. monitor.write("on ")
  100. else
  101. monitor.setCursorPos(17, 17)
  102. monitor.setBackgroundColor(colors.red)
  103. monitor.setTextColor(colors.white)
  104. monitor.write("off")
  105. end
  106. end
  107.  
  108.  
  109.  
  110. drawControls()
  111.  
  112.  
  113. while true do
  114. local event, side, x, y = os.pullEvent("monitor_touch")
  115.  
  116. if x >= 1 and x <= 15 and y >= 15 and y <= 17 then
  117. term.clear()
  118. term.redirect(term.native())
  119.  
  120. print("Where's the music at?")
  121. write("URL:")
  122.  
  123. local url = read()
  124.  
  125. local response = http.get(url, nil, true)
  126.  
  127. local function stringChunks(str)
  128. local chunkSize = 16 * 524
  129. local start = 1
  130. return function()
  131. if start > #str then
  132. return nil
  133. else
  134. local chunk = str:sub(start, start + chunkSize - 1)
  135. start = start + chunkSize
  136. return chunk
  137. end
  138. end
  139. end
  140.  
  141. if response then
  142. local responseData = response.readAll()
  143. response.close()
  144.  
  145. local decoder = dfpwm.make_decoder()
  146. for chunk in stringChunks(responseData) do
  147. local buffer = decoder(chunk)
  148. speaker.playAudio(buffer)
  149. end
  150. songLoaded = true
  151. end
  152.  
  153. term.redirect(monitor)
  154. drawControls()
  155. elseif x >= 1 and x <= 10 and y == 5 then
  156. if isPlaying then
  157. isPlaying = false
  158. speaker.stopAudio()
  159. else
  160. if songLoaded then
  161. isPlaying = true
  162. speaker.playAudio()
  163. end
  164. end
  165. drawControls()
  166. elseif x >= 1 and x <= 3 and y == 12 then
  167. if volume > 0.0 then
  168. volume = volume - 0.1
  169. speaker.setVolume(volume)
  170. end
  171. drawControls()
  172. elseif x >= 16 and x <= 17 and y == 12 then
  173. if volume < 1.0 then
  174. volume = volume + 0.1
  175. speaker.setVolume(volume)
  176. end
  177. drawControls()
  178. end
  179. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement