Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Track = {--{{{
- slot = nil,
- title = nil,
- length = 0,
- trackPos = nil,
- }--}}}
- Jukebox = {--{{{
- drive = peripheral.wrap("drive_1"),
- chest = peripheral.wrap("crystal_chest_0"),
- driveDirection = "east",
- tracks = {},
- trackTimerID = nil,
- currentSlot = 0,
- loop = true,
- playing = false,
- playingFor = 0,
- listNumTracks = 16,
- firstVisible = 0,
- }--}}}
- function Jukebox:play(slot)--{{{
- if slot ~= nil then
- self.currentSlot = slot
- Jukebox:selectSlot(self.currentSlot)
- end
- if self.drive.isDiskPresent() then
- if self.drive.hasAudio() then
- local length = self.tracks[self.currentSlot].length
- if length > 0 then
- self.trackTimerID = os.startTimer(length)
- end
- self.drive.playAudio()
- self.playing = true
- end
- end
- end--}}}
- function Jukebox:randomTrack()--{{{
- end--}}}
- function Jukebox:stop()--{{{
- self.drive.stopAudio()
- self.playing = false
- self.trackTimerID = nil
- end--}}}
- function Jukebox:nextTrack()--{{{
- self.trackTimerID = nil
- if self.currentSlot == #self.tracks then
- return
- end
- self:selectSlot(self.currentSlot + 1)
- end--}}}
- function Jukebox:previousTrack()--{{{
- self.trackTimerID = nil
- if self.currentSlot == 0 then
- return
- end
- self:selectSlot(self.currentSlot - 1)
- end--}}}
- function Jukebox:selectSlot(slot)--{{{
- self.chest.pullIntoSlot(self.driveDirection, 1, 1, self.currentSlot)
- self.currentSlot = slot
- self.chest.pushIntoSlot(self.driveDirection, self.currentSlot, 1, 1)
- end--}}}
- function Jukebox:currentTrackName()--{{{
- if self.drive.isDiskPresent() and self.drive.hasAudio() then
- name = self.drive.getAudioTitle()
- if name == 'pg.stillalive' then
- name = 'Portal: Still Alive'
- elseif name == 'pg.wantyougone' then
- name = 'Portal: Want You Gone'
- elseif name == 'wait' then
- name = 'C418 - wait'
- elseif name == 'Blue' then
- name = 'Eiffle65 - Blue'
- end
- return name
- else
- return 'No disk'
- end
- end--}}}
- function Jukebox:getTrackLength(title)--{{{
- if title == 'Eiffle65 - Blue' then
- return 256
- elseif title == 'Lights' then
- return 200
- elseif title == 'Take On Me' then
- return 226
- elseif title == 'Dragostea Din Tei' then
- return 213
- elseif title == "Now You're Gone" then
- return 210
- elseif title == 'Ghosts N Stuff' then
- return 351
- elseif title == 'Rainbow Factory' then
- return 207
- elseif title == 'Payphone' then
- return 232
- elseif title == 'Somebody That I Use To Know' then
- return 242
- elseif title == 'Dont Fear The Reaper' then
- return 290
- elseif title == 'Portal: Still Alive' then
- return 176
- elseif title == 'Portal: Want You Gone' then
- return 141
- elseif title == 'C418 - blocks' then
- return 345
- elseif title == 'C418 - chirp' then
- return 185
- elseif title == 'C418 - mall' then
- return 197
- elseif title == 'C418 - mellohi' then
- return 96
- elseif title == 'C418 - strad' then
- return 188
- elseif title == 'C418 - 13' then
- return 178
- elseif title == 'C418 - cat' then
- return 185
- elseif title == 'C418 - far' then
- return 174
- elseif title == 'C418 - stal' then
- return 150
- elseif title == 'C418 - ward' then
- return 251
- elseif title == 'C418 - 11' then
- return 71
- elseif title == 'C418 - wait' then
- return 238
- elseif title == 'Smile Smile Smile' then
- return 200
- elseif title == 'September' then
- return 172
- elseif title == 'All The Small Things' then
- return 180
- elseif title == 'Carry On My Wayward Son' then
- return 109
- elseif title == 'Killer Queen' then
- return 180
- elseif title == 'Moves Like Jagger' then
- return 211
- elseif title == 'Sail' then
- return 223
- elseif title == 'Moonlit Marathon' then
- return 208
- elseif title == 'Twenty One Guns' then
- return 304
- elseif title == 'Paper Wings' then
- return 215
- elseif title == 'Whirlybird' then
- return 233
- elseif title == 'Beyond Her Garden' then
- return 163
- elseif title == 'Daddy Discord' then
- return 271
- elseif title == 'Love Is In Bloom' then
- return 86
- elseif title == 'Call Me Maybe' then
- return 174
- else
- return 0
- end
- end--}}}
- function Jukebox:resetTerm()--{{{
- term.clear()
- term.setCursorPos(1,1)
- end--}}}
- function Jukebox:scanTracks()--{{{
- self:resetTerm()
- print("Scanning tracks")
- self.tracks = {}
- local slotBefore = self.currentSlot
- for slot=0,self.chest.getSizeInventory()-1 do
- self:selectSlot(slot)
- name = self:currentTrackName()
- if name ~= 'No disk' and name ~= nil then
- local track = {}
- track.slot = slot
- track.title = name
- track.length = self:getTrackLength(track.title)
- self.tracks[slot] = track
- print("Slot " .. slot .. " is " .. track.title)
- else
- break
- end
- end
- self:selectSlot(slotBefore)
- print("Found " .. #self.tracks)
- if self.playing then
- self:play()
- end
- end--}}}
- function Jukebox:listTracks()--{{{
- local showLast = 0
- self:resetTerm()
- print("Number of tracks: " .. tostring(#self.tracks))
- if (self.currentSlot - (self.listNumTracks / 2)) < 0 then
- self.firstVisible = 0
- else
- self.firstVisible = self.currentSlot - (self.listNumTracks / 2)
- end
- if (self.firstVisible + self.listNumTracks) < #self.tracks then
- showLast = self.firstVisible+self.listNumTracks
- else
- self.firstVisible = #self.tracks - self.listNumTracks
- showLast = #self.tracks
- end
- for slot=self.firstVisible,showLast do
- termX, termY = term.getCursorPos()
- track = self.tracks[slot]
- if self.currentSlot == track.slot then
- term.setCursorPos(1, termY)
- io.write('>')
- end
- term.setCursorPos(termX+1, termY)
- output = tostring(track.slot)
- if output:len() < 2 then
- output = "0" .. output
- end
- output = output .. ": " .. track.title .. ' (' .. math.floor(track.length / 60) .. 'm:' .. tostring(track.length % 60) .. 's)'
- print(output)
- end
- end--}}}
- function Jukebox:reset()--{{{
- -- clears the disk drive of any records that may be in there.
- -- check for record in drive.
- if self.drive.isDiskPresent() then
- -- if disk in drive find first free slot in chest.
- local freeSlot = self:findFreeSlot()
- -- insert disk into free slot.
- self.chest.pullIntoSlot(self.driveDirection, 1, 1, freeSlot)
- end
- end--}}}
- function Jukebox:findFreeSlot()--{{{
- for slot=0,self.chest.getSizeInventory()-1 do
- if not self.chest.getStackInSlot(slot) then
- return slot
- end
- end
- end--}}}
- function Jukebox:debugInfo()--{{{
- self:resetTerm()
- print(textutils.serialize(self.currentSlot))
- print(textutils.serialize(self.tracks[self.currentSlot]))
- end--}}}
- jukebox = Jukebox
- jukebox:reset()
- jukebox:scanTracks()
- jukebox:listTracks()
- os.startTimer(1)
- while true do
- event, param1, param2, param3 = os.pullEvent()
- if event == 'timer' then
- if param1 == jukebox.trackTimerID then
- if jukebox.currentSlot == #jukebox.tracks and jukebox.loop then
- jukebox:selectSlot(0)
- else
- jukebox:nextTrack()
- end
- jukebox:play()
- jukebox:listTracks()
- end
- elseif event == "key" then
- --print(tostring(param1) .. " was pressed")
- if param1 == 44 or param1 == 200 then -- z or up cursor
- jukebox:previousTrack()
- jukebox:listTracks()
- elseif param1 == 45 then -- x
- jukebox:stop()
- elseif param1 == 46 then -- c
- jukebox:play()
- elseif param1 == 47 or param1 == 208 then -- v or down cursor
- jukebox:nextTrack()
- jukebox:listTracks()
- elseif param1 == 30 then -- a
- jukebox:scanTracks()
- elseif param1 == 38 then -- l
- jukebox:listTracks()
- end
- if param1 == 37 then -- k
- jukebox:debugInfo()
- else
- jukebox:listTracks()
- end
- elseif event == 'monitor_touch' then
- print(param1 .. ", " .. param2 .. ", " .. param3)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement