Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require("component")
- local fs = require("filesystem")
- local shell = require("shell")
- local computer = require("computer")
- local song = {}
- function getByte(f)
- return string.byte(f:read(1))
- end
- function readString(file)
- local strln = get_int_32(getByte(file),getByte(file),getByte(file),getByte(file))
- local str = ""
- str = file:read(strln)
- return str
- end
- function get_int_16(b1, b2)
- local n = b1 + 256 * b2
- n = (n > 32767) and (n - 32768) or n
- return n
- end
- function get_int_32(b1, b2, b3, b4)
- if not b4 then error("need four bytes to convert to int",2) end
- local n = b1 + b2*256 + b3*65536 + b4*16777216
- n = (n > 2147483647) and (n - 4294967296) or n
- return n
- end
- function song:loadSong(path)
- if not fs.exists(path) then error("File \"" .. path .. "\" does not exist.\n") end
- local file = io.open(path,"rb")
- self.length = get_int_16(getByte(file),getByte(file))
- self.height = get_int_16(getByte(file),getByte(file))
- self.name = readString(file)
- self.author = readString(file)
- self.org_author = readString(file)
- self.description = readString(file)
- self.tempo = get_int_16(getByte(file),getByte(file)) * 0.01
- print("Name: " .. self.name)
- print("Description: " .. self.description)
- print("Length in ticks: " .. self.length)
- self.song_ticks = {}
- local tick = -1
- local jumps = 0
- local counter = 1
- while true do
- jumps = get_int_16(getByte(file),getByte(file))
- print("Jumps: " .. jumps)
- if jumps == 0 then
- break
- end
- tick = tick + jumps
- local layer = -1
- while true do
- self.song_ticks[counter] = {}
- jumps = get_int_16(getByte(file),getByte(file))
- if jumps == 0 then
- break
- end
- layer = layer + jumps
- self.song_ticks[counter].instrument = getByte(file)
- self.song_ticks[counter].note = getByte(file) - 33
- self.song_ticks[counter].layer = layer
- self.song_ticks[counter].tick = tick
- print("cticks: " .. self.song_ticks[counter].tick)
- counter = counter + 1
- end
- end
- file:close()
- end
- function song:play_note(cur_tick, position)
- while true do
- if self.song_ticks[position].tick == cur_tick then
- computer.beep(self.song_ticks[position].note * 100)
- else
- break
- end
- end
- end
- print("started loading")
- song:loadSong(...)
- print("finished loading")
- local finished = false
- local current_tick = 0
- while not finished do
- current_tick = current_tick + 1
- os.sleep(1/song.tempo)
- if current_tick == song.length then
- finished = true
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement