Advertisement
metalim

play

Jun 8th, 2013
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.09 KB | None | 0 0
  1. -- http://dl.dropboxusercontent.com/s/2lfujs9n53sijnx/play
  2. -- http://pastebin.com/xXdBcZRW
  3. local function update()
  4.     local i=http.get('http://dl.dropboxusercontent.com/s/2lfujs9n53sijnx/play')
  5.     local o=io.open(shell.getRunningProgram(),'w')
  6.     o:write(i.readAll())
  7.     o:write('\n')
  8.     o:close()
  9. end
  10.  
  11. local args={...}
  12. if args[1]=='update' then
  13.     update()
  14.     return
  15. end
  16.  
  17. local drive=peripheral.wrap('front')
  18. local songs={
  19.     ['C418 - 13']=178,
  20.     ['C418 - cat']=185,
  21.     ['C418 - blocks']=345,
  22.     ['C418 - chirp']=185,
  23.     ['C418 - far']=174,
  24.     ['C418 - mall']=197,
  25.     ['C418 - mellohi']=96,
  26.     ['C418 - stal']=150,
  27.     ['C418 - strad']=188,
  28.     ['C418 - ward']=254,
  29.     ['C418 - 11']=71,
  30.     ['C418 - wait']=238,
  31.     ['Valve - Still Alive']=176,
  32.     ['Valve - Radio Loop']=21,
  33.     ['Valve - Want You Gone']=141,
  34.     ['artist - song']=0,
  35. }
  36.  
  37. local player
  38. player = {
  39.     playing = true,
  40.     slot = 1,
  41.     play = function()
  42.         player.playing = true
  43.         if turtle.getItemCount(player.slot)>0 then
  44.             if drive.hasAudio() then turtle.suck() end
  45.             turtle.select(player.slot)
  46.             turtle.drop()
  47.         end
  48.         drive.playAudio()
  49.     end,
  50.     pause = function()
  51.         if player.playing then drive.stopAudio() else drive.playAudio() end
  52.         player.playing = not player.playing
  53.     end,
  54.     stop = function()
  55.         turtle.suck()
  56.         player.playing = false
  57.     end,
  58.     skip = function(step)
  59.         player.stop()
  60.         step = step or 1
  61.         repeat
  62.             player.slot = player.slot + step
  63.             if player.slot > 16 then player.slot = player.slot - 16 end
  64.             if player.slot < 1 then player.slot = player.slot + 16 end
  65.         until turtle.getItemCount(player.slot) > 0
  66.         player.play()
  67.     end,
  68.     display = function()
  69.         if player.playing then
  70.             local title=drive.getAudioTitle()
  71.             local length=songs[title] or 60
  72.             print(textutils.formatTime(length/60, true)..' '..title)
  73.             local w,h = term.getSize()
  74.             local x,y = term.getCursorPos()
  75.             local lengthFormatted=textutils.formatTime(length/60, true)
  76.             local barLength = w-x-3-#lengthFormatted
  77.             for time=0,length do
  78.                 local barFilled = math.floor(barLength*time/length)
  79.                 local timeFormatted=textutils.formatTime(time/60, true)
  80.                 term.setCursorPos(x,y)
  81.                 write(timeFormatted)
  82.                 write(' [')
  83.                 write(string.rep('#',barFilled))
  84.                 write(string.rep('-',barLength-barFilled))
  85.                 write(']')
  86.                 sleep(1)
  87.             end
  88.             print('')
  89.         else
  90.             while true do sleep(60) end
  91.         end
  92.     end,
  93.     controller = function()
  94.         while true do
  95.             local event, scancode = os.pullEvent('key')
  96.             --print(scancode)
  97.             if scancode==57 then -- space
  98.                 player.pause()
  99.                 return
  100.             elseif scancode==203 then -- left
  101.                 player.skip(-1)
  102.                 return
  103.             elseif scancode==205 then -- right
  104.                 player.skip(1)
  105.                 return
  106.             elseif scancode==200 then -- up
  107.                 player.skip(-4)
  108.                 return
  109.             elseif scancode==208 then -- down
  110.                 player.skip(4)
  111.                 return
  112.             elseif scancode==16 then -- 'q'
  113.                 player.stop()
  114.                 player.exit = true
  115.                 return
  116.             end
  117.         end
  118.     end,
  119.     run = function()
  120.         player.play()
  121.         repeat
  122.             done = parallel.waitForAny( player.display, player.controller )
  123.             if done == 1 then player.skip() end
  124.         until player.exit
  125.     end,
  126. }
  127.  
  128. player.run()
  129.  
  130. print('')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement