Advertisement
demon012

jukeboxComputer

Jul 23rd, 2013
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.80 KB | None | 0 0
  1. Track = {--{{{
  2.     slot = nil,
  3.     title = nil,
  4.     length = 0,
  5.     trackPos = nil,
  6. }--}}}
  7. Jukebox = {--{{{
  8.     drive = peripheral.wrap("drive_1"),
  9.     chest = peripheral.wrap("crystal_chest_0"),
  10.     driveDirection = "east",
  11.     tracks = {},
  12.     trackTimerID = nil,
  13.     currentSlot = 0,
  14.     loop = true,
  15.     playing = false,
  16.     playingFor = 0,
  17.     listNumTracks = 16,
  18.     firstVisible = 0,
  19. }--}}}
  20. function Jukebox:play(slot)--{{{
  21.     if slot ~= nil then
  22.         self.currentSlot = slot
  23.         Jukebox:selectSlot(self.currentSlot)
  24.     end
  25.  
  26.     if self.drive.isDiskPresent() then
  27.         if self.drive.hasAudio() then
  28.             local length = self.tracks[self.currentSlot].length
  29.             if length > 0 then
  30.                 self.trackTimerID = os.startTimer(length)
  31.             end
  32.             self.drive.playAudio()
  33.             self.playing = true
  34.         end
  35.     end
  36. end--}}}
  37. function Jukebox:randomTrack()--{{{
  38. end--}}}
  39. function Jukebox:stop()--{{{
  40.     self.drive.stopAudio()
  41.     self.playing = false
  42.     self.trackTimerID = nil
  43. end--}}}
  44. function Jukebox:nextTrack()--{{{
  45.     self.trackTimerID = nil
  46.     if self.currentSlot == #self.tracks then
  47.         return
  48.     end
  49.     self:selectSlot(self.currentSlot + 1)
  50. end--}}}
  51. function Jukebox:previousTrack()--{{{
  52.     self.trackTimerID = nil
  53.     if self.currentSlot == 0 then
  54.         return
  55.     end
  56.     self:selectSlot(self.currentSlot - 1)
  57. end--}}}
  58. function Jukebox:selectSlot(slot)--{{{
  59.     self.chest.pullIntoSlot(self.driveDirection, 1, 1, self.currentSlot)
  60.     self.currentSlot = slot
  61.     self.chest.pushIntoSlot(self.driveDirection, self.currentSlot, 1, 1)
  62. end--}}}
  63. function Jukebox:currentTrackName()--{{{
  64.     if self.drive.isDiskPresent() and self.drive.hasAudio() then
  65.         name = self.drive.getAudioTitle()
  66.         if name == 'pg.stillalive' then
  67.             name = 'Portal: Still Alive'
  68.         elseif name == 'pg.wantyougone' then
  69.             name = 'Portal: Want You Gone'
  70.         elseif name == 'wait' then
  71.             name = 'C418 - wait'
  72.         elseif name == 'Blue' then
  73.             name = 'Eiffle65 - Blue'
  74.         end
  75.         return name
  76.     else
  77.         return 'No disk'
  78.     end
  79. end--}}}
  80. function Jukebox:getTrackLength(title)--{{{
  81.     if title == 'Eiffle65 - Blue' then
  82.         return 256
  83.     elseif title == 'Lights' then
  84.         return 200
  85.     elseif title == 'Take On Me' then
  86.         return 226
  87.     elseif title == 'Dragostea Din Tei' then
  88.         return 213
  89.     elseif title == "Now You're Gone" then
  90.         return 210
  91.     elseif title == 'Ghosts N Stuff' then
  92.         return 351
  93.     elseif title == 'Rainbow Factory' then
  94.         return 207
  95.     elseif title == 'Payphone' then
  96.         return 232
  97.     elseif title == 'Somebody That I Use To Know' then
  98.         return 242
  99.     elseif title == 'Dont Fear The Reaper' then
  100.         return 290
  101.     elseif title == 'Portal: Still Alive' then
  102.         return 176
  103.     elseif title == 'Portal: Want You Gone' then
  104.         return 141
  105.     elseif title == 'C418 - blocks' then
  106.         return 345
  107.     elseif title == 'C418 - chirp' then
  108.         return 185
  109.     elseif title == 'C418 - mall' then
  110.         return 197
  111.     elseif title == 'C418 - mellohi' then
  112.         return 96
  113.     elseif title == 'C418 - strad' then
  114.         return 188
  115.     elseif title == 'C418 - 13' then
  116.         return 178
  117.     elseif title == 'C418 - cat' then
  118.         return 185
  119.     elseif title == 'C418 - far' then
  120.         return 174
  121.     elseif title == 'C418 - stal' then
  122.         return 150
  123.     elseif title == 'C418 - ward' then
  124.         return 251
  125.     elseif title == 'C418 - 11' then
  126.         return 71
  127.     elseif title == 'C418 - wait' then
  128.         return 238
  129.     elseif title == 'Smile Smile Smile' then
  130.         return 200
  131.     elseif title == 'September' then
  132.         return 172
  133.     elseif title == 'All The Small Things' then
  134.         return 180
  135.     elseif title == 'Carry On My Wayward Son' then
  136.         return 109
  137.     elseif title == 'Killer Queen' then
  138.         return 180
  139.     elseif title == 'Moves Like Jagger' then
  140.         return 211
  141.     elseif title == 'Sail' then
  142.         return 223
  143.     elseif title == 'Moonlit Marathon' then
  144.         return 208
  145.     elseif title == 'Twenty One Guns' then
  146.         return 304
  147.     elseif title == 'Paper Wings' then
  148.         return 215
  149.     elseif title == 'Whirlybird' then
  150.         return 233
  151.     elseif title == 'Beyond Her Garden' then
  152.         return 163
  153.     elseif title == 'Daddy Discord' then
  154.         return 271
  155.     elseif title == 'Love Is In Bloom' then
  156.         return 86
  157.     elseif title == 'Call Me Maybe' then
  158.         return 174
  159.     else
  160.         return 0
  161.     end
  162. end--}}}
  163. function Jukebox:resetTerm()--{{{
  164.     term.clear()
  165.     term.setCursorPos(1,1)
  166. end--}}}
  167. function Jukebox:scanTracks()--{{{
  168.     self:resetTerm()
  169.     print("Scanning tracks")
  170.     self.tracks = {}
  171.     local slotBefore = self.currentSlot
  172.     for slot=0,self.chest.getSizeInventory()-1 do
  173.         self:selectSlot(slot)
  174.         name = self:currentTrackName()
  175.         if name ~= 'No disk' and name ~= nil then
  176.             local track = {}
  177.             track.slot = slot
  178.             track.title = name
  179.             track.length = self:getTrackLength(track.title)
  180.             self.tracks[slot] = track
  181.             print("Slot " .. slot .. " is " .. track.title)
  182.         else
  183.             break
  184.         end
  185.     end
  186.     self:selectSlot(slotBefore)
  187.     print("Found " .. #self.tracks)
  188.     if self.playing then
  189.         self:play()
  190.     end
  191. end--}}}
  192. function Jukebox:listTracks()--{{{
  193.     local showLast = 0
  194.  
  195.     self:resetTerm()
  196.     print("Number of tracks: " .. tostring(#self.tracks))
  197.  
  198.     if (self.currentSlot - (self.listNumTracks / 2)) < 0 then
  199.         self.firstVisible = 0
  200.     else
  201.         self.firstVisible = self.currentSlot - (self.listNumTracks / 2)
  202.     end
  203.  
  204.     if (self.firstVisible + self.listNumTracks) < #self.tracks then
  205.         showLast = self.firstVisible+self.listNumTracks
  206.     else
  207.         self.firstVisible = #self.tracks - self.listNumTracks
  208.         showLast = #self.tracks
  209.     end
  210.     for slot=self.firstVisible,showLast do
  211.         termX, termY = term.getCursorPos()
  212.         track = self.tracks[slot]
  213.  
  214.         if self.currentSlot == track.slot then
  215.             term.setCursorPos(1, termY)
  216.             io.write('>')
  217.         end
  218.  
  219.         term.setCursorPos(termX+1, termY)
  220.         output = tostring(track.slot)
  221.         if output:len() < 2 then
  222.             output = "0" .. output
  223.         end
  224.         output = output .. ": " ..  track.title .. ' (' .. math.floor(track.length / 60) .. 'm:' .. tostring(track.length % 60) .. 's)'
  225.         print(output)
  226.  
  227.     end
  228. end--}}}
  229. function Jukebox:reset()--{{{
  230.     -- clears the disk drive of any records that may be in there.
  231.     -- check for record in drive.
  232.     if self.drive.isDiskPresent() then
  233.         -- if disk in drive find first free slot in chest.
  234.         local freeSlot = self:findFreeSlot()
  235.         -- insert disk into free slot.
  236.         self.chest.pullIntoSlot(self.driveDirection, 1, 1, freeSlot)
  237.     end
  238. end--}}}
  239. function Jukebox:findFreeSlot()--{{{
  240.     for slot=0,self.chest.getSizeInventory()-1 do
  241.         if not self.chest.getStackInSlot(slot) then
  242.             return slot
  243.         end
  244.     end
  245. end--}}}
  246. function Jukebox:debugInfo()--{{{
  247.     self:resetTerm()
  248.     print(textutils.serialize(self.currentSlot))
  249.     print(textutils.serialize(self.tracks[self.currentSlot]))
  250. end--}}}
  251.  
  252. jukebox = Jukebox
  253. jukebox:reset()
  254. jukebox:scanTracks()
  255. jukebox:listTracks()
  256.  
  257. os.startTimer(1)
  258.  
  259. while true do
  260.     event, param1, param2, param3 = os.pullEvent()
  261.     if event ==  'timer' then
  262.         if param1 == jukebox.trackTimerID then
  263.             if jukebox.currentSlot == #jukebox.tracks and jukebox.loop then
  264.                 jukebox:selectSlot(0)
  265.             else
  266.                 jukebox:nextTrack()
  267.             end
  268.             jukebox:play()
  269.             jukebox:listTracks()
  270.         end
  271.     elseif event == "key" then
  272.         --print(tostring(param1) .. " was pressed")
  273.         if param1 == 44 or param1 == 200 then -- z or up cursor
  274.             jukebox:previousTrack()
  275.             jukebox:listTracks()
  276.         elseif param1 == 45 then -- x
  277.             jukebox:stop()
  278.         elseif param1 == 46 then -- c
  279.             jukebox:play()
  280.         elseif param1 == 47 or param1 == 208 then -- v or down cursor
  281.             jukebox:nextTrack()
  282.             jukebox:listTracks()
  283.         elseif param1 == 30 then -- a
  284.             jukebox:scanTracks()
  285.         elseif param1 == 38 then -- l
  286.             jukebox:listTracks()
  287.         end
  288.         if param1 == 37 then -- k
  289.             jukebox:debugInfo()
  290.         else
  291.             jukebox:listTracks()
  292.         end
  293.     elseif event == 'monitor_touch' then
  294.         print(param1 .. ", " .. param2 .. ", " .. param3)
  295.     end
  296. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement