Thetechnicbrick

Finished Jukebox

Mar 23rd, 2015
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.84 KB | None | 0 0
  1. -- Configure these
  2. discSide = "front"
  3.  
  4. -- Don't touch these
  5. discs = {}
  6. discs["C418 - 11"] = {duration=75, title="C418 - 11"}
  7. discs["C418 - 13"] = {duration=178, title="C418 - 13"}
  8. discs["C418 - blocks"] = {duration=355, title="C418 - Blocks"}
  9. discs["C418 - cat"] = {duration=188, title="C418 - Cat"}
  10. discs["C418 - chirp"] = {duration=186, title="C418 - Chirp"}
  11. discs["C418 - far"] = {duration=172, title="C418 - Far"}
  12. discs["C418 - mall"] = {duration=200, title="C418 - Mall"}
  13. discs["C418 - mellohi"] = {duration=99, title="C418 - Mellohi"}
  14. discs["C418 - stal"] = {duration=156, title="C418 - Stal"}
  15. discs["C418 - strad"] = {duration=193, title="C418 - Strad"}
  16. discs["C418 - wait"] = {duration=235, title="C418 - Wait"}
  17. discs["C418 - ward"] = {duration=251, title="C418 - Ward"}
  18. discs["pg.radioloop"] = {duration=25, title="Valve - Radio Loop"}
  19. discs["pg.stillalive"] = {duration=175, title="Valve - Still Alive"}
  20. discs["pg.wantyougone"] = {duration=135, title="Valve - Want You Gone"}
  21.  
  22. currentSlot = turtle.getSelectedSlot()
  23. currentTimer = nil
  24. startTime = 0
  25. jbRunTime = 0
  26. discInfo = nil
  27. state = "startup"
  28. monitor = peripheral.find("monitor")
  29. activeslots = 0
  30.  
  31.  
  32. for i=1,16 do
  33. if turtle.getItemCount(i) == 1 then
  34. activeslots = activeslots + 1
  35. end
  36. end
  37.  
  38.  
  39.  
  40. function incSlot(amount)
  41. currentSlot = currentSlot + amount
  42. if currentSlot > activeslots then
  43. currentSlot = currentSlot - activeslots
  44. elseif currentSlot == 0 then
  45. currentSlot = activeslots
  46. end
  47. return currentSlot
  48. end
  49.  
  50. function changeDisk(dir)
  51. if dir == nil then
  52. dir = 1
  53. elseif dir == 99 then
  54. dir = math.random(activeslots - 1)
  55. end
  56. haveDisk = disk.isPresent(discSide)
  57.  
  58. if haveDisk then
  59. turtle.suck()
  60. end
  61.  
  62. slot = incSlot(dir)
  63. while turtle.getItemCount(slot) < 1 do
  64. slot = incSlot(dir)
  65. end
  66.  
  67. turtle.select(currentSlot)
  68. turtle.drop()
  69.  
  70. end
  71.  
  72. function stopPlayback(quiet)
  73. if disk.isPresent(discSide) and disk.hasAudio(discSide) then
  74. disk.stopAudio(discSide)
  75. currentTimer = nil
  76. startTime = 0
  77. print("Stopped")
  78. monitor.clear()
  79. monitor.setCursorPos(1,1)
  80. monitor.setTextColor(colors.gray)
  81. monitor.write("Stopped...")
  82. state="stopped"
  83. renderFooter(state)
  84. end
  85. end
  86.  
  87. function playCurrentDisc()
  88. title = disk.getAudioTitle(discSide)
  89. discInfo = discs[title]
  90. if not discInfo then
  91. print("No disc info found for " .. title)
  92. changeDisk(1)
  93. end
  94. term.clear()
  95. term.setCursorPos(1,2)
  96. print("Playing " .. discInfo["title"] .. " for " .. discInfo["duration"] .. " seconds")
  97. if not(state == "playing") then
  98. monitor.clear()
  99. monitor.setCursorPos(1,1)
  100. monitor.setTextColor(colors.gray)
  101. monitor.write("Now Playing:")
  102. state="playing"
  103. renderFooter(state)
  104. end
  105. monitor.setCursorPos(2,2)
  106. monitor.clearLine()
  107. monitor.setTextColor(colors.white)
  108. monitor.write(discInfo["title"])
  109. jbRunTime = discInfo["duration"]
  110.  
  111. disk.playAudio(discSide)
  112. currentTimer = os.startTimer(discInfo["duration"])
  113. startTime = os.clock()
  114. renderDuration()
  115. print("[P]lay, [F]orward, [B]ack, [S]top")
  116.  
  117. end
  118.  
  119. function renderButton(text, active, state)
  120. if state == active then
  121. monitor.setTextColor(colors.green)
  122. monitor.write(text)
  123. else
  124. monitor.setTextColor(colors.gray)
  125. monitor.write(text)
  126. end
  127. end
  128.  
  129. function renderFooter(state)
  130. monitor.setCursorPos(1,5)
  131. monitor.clearLine()
  132.  
  133. renderButton("[]", "stopped", state)
  134. renderButton("<<", "rewind", state)
  135. renderButton("[?]", "random", state)
  136. renderButton(">>", "forward", state)
  137. renderButton("[>", "playing", state)
  138.  
  139. end
  140.  
  141. function checkButton(x, y)
  142. if y == 5 then
  143. if x == 1 or x == 2 then
  144. stopPlayback()
  145. elseif x == 3 or x == 4 then
  146. stopPlayback(true)
  147. changeDisk(-1)
  148. playCurrentDisc()
  149. elseif x == 5 or x == 6 or x == 7 then
  150. stopPlayback(true)
  151. changeDisk(99)
  152. playCurrentDisc()
  153. elseif x == 8 or x == 9 then
  154. stopPlayback(true)
  155. changeDisk(1)
  156. playCurrentDisc()
  157. elseif x == 10 or x == 11 then
  158. if currentTimer == nil then
  159. startPlayback()
  160. else
  161. print("Already playing")
  162. end
  163. end
  164. renderFooter(state)
  165. end
  166. end
  167.  
  168. function renderDuration()
  169. monitor.setCursorPos(2,3)
  170. monitor.clearLine()
  171. local timeLeft = math.floor(os.clock() - startTime)
  172. if timeLeft > discInfo["duration"] then
  173. timeLeft = discInfo["duration"]
  174. end
  175. monitor.setTextColor(colors.white)
  176. monitor.write(time(timeLeft) .. "/" .. time(discInfo["duration"]))
  177. end
  178.  
  179. function startPlayback()
  180. if disk.isPresent(discSide) and disk.hasAudio(discSide) then
  181. playCurrentDisc()
  182. else
  183. changeDisk(1)
  184. playCurrentDisc()
  185. end
  186. end
  187.  
  188. function time(sec)
  189. min = math.floor((sec/60))
  190. secs = sec - (min * 60)
  191. if secs < 10 then
  192. secs = "0" .. secs
  193. end
  194. form = min .. ":" .. secs
  195. return form
  196. end
  197.  
  198. countDownTimer = os.startTimer(1)
  199.  
  200. startPlayback()
  201.  
  202. while true do
  203. ev,p1,p2,p3 = os.pullEvent()
  204. if state == "playing" then
  205. -- Placed outside of timer loop to fix issues with timer in multiplayer
  206. tempTimer = os.startTimer(1)
  207. renderDuration()
  208. end
  209. if ev == "timer" then
  210.  
  211.  
  212. if currentTimer == p1 and disk.isPresent(discSide) and disk.hasAudio(discSide) then
  213. stopPlayback(true)
  214. changeDisk(1)
  215. playCurrentDisc()
  216. end
  217. elseif ev == "char" then
  218. if p1 == 'p' then
  219. if currentTimer == nil then
  220. startPlayback()
  221. else
  222. print("Already playing")
  223. end
  224. elseif p1 == 'f' then
  225. stopPlayback(true)
  226. changeDisk(1)
  227. playCurrentDisc()
  228. elseif p1 == 'b' then
  229. stopPlayback(true)
  230. changeDisk(-1)
  231. playCurrentDisc()
  232. elseif p1 == 's' then
  233. stopPlayback()
  234. elseif p1 == 'r' then
  235. stopPlayback(true)
  236. changeDisk(99)
  237. playCurrentDisc()
  238. end
  239. elseif ev == "monitor_touch" then
  240. checkButton(p2, p3)
  241. end
  242. end
Advertisement
Add Comment
Please, Sign In to add comment