Advertisement
Brick

ComputerCraft Auto-Jukebox

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