Advertisement
CaptainSpaceCat

Spodify

Dec 26th, 2015
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.68 KB | None | 0 0
  1. local w, h = term.getSize()
  2. local dSide = "left"
  3. local mainColor = colors.white
  4. local active = false
  5. local repeat_ = false
  6. local songtimer
  7. local songcount = 0
  8.  
  9. local diskList = {
  10.     ["C418 - cat"] = 178,
  11.     ["C418 - 13"] = 185,
  12.     ["C418 - stal"] = 150,
  13.     ["C418 - strad"] = 188,
  14.     ["C418 - wait"] = 238,
  15.     ["C418 - mall"] = 197,
  16.     ["C418 - mellohi"] = 96,
  17.     ["C418 - far"] = 174,
  18.     ["C418 - ward"] = 251,
  19.     ["C418 - blocks"] = 345,
  20.     ["C418 - chirp"] = 185,
  21.     ["C418 - 11"] = 77
  22. }
  23.  
  24. --in case there are any changes to the original disks by mods
  25. diskList["C418 - wait"] = 171
  26. diskList["C418 - 11"] = 246
  27.  
  28. function printMedia()
  29.   term.setBackgroundColor(colors.black)
  30.   term.setTextColor(colors.white)
  31.   term.clear()
  32.   term.setCursorPos(1, 1)
  33.   print("Media Player 9001\n")
  34.   if disk.hasAudio("left") then
  35.     print("  " .. disk.getAudioTitle(dSide))
  36.     print("  Length: " .. getTimeString(diskList[disk.getAudioTitle(dSide)]) .. "\n")
  37.     print("Press Space to Play/Pause")
  38.     print("Press Tab to Eject")
  39.     print("Press LShift to Toggle Repeat")
  40.     term.setCursorPos(1, h - 2)
  41.     local rep = repeat_ and "On" or "Off"
  42.     write("Repeat: " .. rep)
  43.     term.setCursorPos(1, h - 1)
  44.     if active then
  45.       write("Playing...")
  46.       term.setCursorPos(w - 3, h - 1)
  47.       write(getTimeString(songcount))
  48.       updateProgressBar(mainColor)
  49.     else
  50.       write("Stopped")
  51.     end
  52.   else
  53.     print("Please insert disc")
  54.   end
  55. end
  56.  
  57. function updateProgressBar(col)
  58.     col = col or colors.white
  59.     term.setBackgroundColor(col)
  60.     term.setCursorPos(1, h)
  61.     write(string.rep(" ", songcount/diskList[disk.getAudioTitle(dSide)] * w))
  62. end
  63.  
  64. function getTimeString(t)
  65.   local sNum = t % 60 > 9 and t % 60 or "0" .. t % 60
  66.   return math.floor(t / 60) .. ":" .. sNum
  67. end
  68.  
  69. while true do
  70.   printMedia()
  71.   local e,k = os.pullEvent()
  72.   if k == keys.space then
  73.     if not active then
  74.       songcount = 0
  75.       songtimer = os.startTimer(1)
  76.       disk.playAudio(dSide)
  77.     else
  78.       songtimer = nil
  79.       disk.stopAudio(dSide)
  80.     end
  81.     active = not active
  82.   elseif k == keys.tab then
  83.     songtimer = nil
  84.     active = false
  85.     disk.stopAudio("left")
  86.     disk.eject(dSide)
  87.   elseif k == keys.leftShift then
  88.     repeat_ = not repeat_
  89.   end
  90.   if e == "timer" and k == songtimer then
  91.     songtimer = os.startTimer(1)
  92.     songcount = songcount + 1
  93.     if songcount == diskList[disk.getAudioTitle(dSide)] then
  94.       if repeat_ then
  95.         disk.stopAudio(dSide)
  96.         songcount = 0
  97.         songtimer = os.startTimer(1)
  98.         disk.playAudio(dSide)
  99.       else
  100.         disk.stopAudio(dSide)
  101.         songcount = 0
  102.         songtimer = nil
  103.         active = false
  104.       end
  105.     end
  106.   end
  107. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement