Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- term.clear()
- term.setCursorPos(1,1)
- filename = "Songs/Vanilla Twilight.nbs"
- nb = {}
- song = {}
- insts = {"note.harp","note.bassattack","note.bd","note.snare","note.hat"}
- volume = {1, 0.5, 0.4, 0.3, 0.3}
- for i = 0,24 do
- nb[i] = 0.5*(2^(i/12))
- end
- notes = {}
- file = fs.open(filename,"rb")
- if not file then error("Failed to open file") end
- function readByte()
- return file.read()
- end
- function bytes_to_int(a,b,c,d)--little Endian
- sum = 0
- sum = sum + a
- sum = sum + b * 2 ^ 8
- sum = sum + c * 2 ^ 16
- sum = sum + d * 2 ^ 24
- return sum
- end
- function readInt()
- e = readByte()
- f = readByte()
- g = readByte()
- h = readByte()
- return bytes_to_int(e,f,g,h)
- end
- function readShort()
- g = readByte()
- h = readByte()
- e = 0
- f = 0
- return bytes_to_int(g,h,e,f)
- end
- function readString()
- str = ""
- length = readInt()
- for i = 1,length do
- str = str..string.char(readByte())
- end
- return str
- end
- function readHeader()
- Length = readShort()
- height = readShort()
- name = readString()
- author = readString()
- originalAuthor = readString()
- description = readString()
- tempo = readShort()
- autoSave = readByte()
- autoSaveDuration = readByte()
- timeSig = readByte()
- minutesSpent = readInt()
- LeftClicks = readInt()
- rightClicks = readInt()
- blocksAdded = readInt()
- blocksRemoved = readInt()
- OrigionalFile = readString()
- end
- function addNoteBlock(tick, layer, instrument, key)
- if not song[tick] then
- song[tick] = {}
- song[tick][1] = {insts[instrument+1],nb[key-33],volume[instrument+1]}
- else
- --print(#song[tick]+1)
- song[tick][#song[tick]+1] = {insts[instrument+1],nb[key-33],volume[instrument+1]}
- end
- end
- function readSong()
- tick = -1
- jumps = 0
- while true do
- jumps = readShort()
- if jumps == 0 then
- break
- end
- tick = tick + jumps
- layer = -1
- while true do
- jumps = readShort()
- if jumps == 0 then
- break
- end
- layer = layer + jumps
- inst = readByte()
- key = readByte()
- addNoteBlock(tick, layer, inst, key)
- end
- end
- end
- function progress(p)
- --51 pixels
- count = 0
- --line 5
- term.setCursorPos(1,19)
- write(math.floor((p/Length)*100).."% Done")
- term.setCursorPos(1,18)
- percent = p/Length
- write("<"..string.rep("=",math.floor(49*percent))..string.rep("-",math.ceil(49-(49*percent)))..">")
- end
- readHeader()
- readSong()
- print(name)
- print(author)
- print(description)
- print((tempo/100).." Ticks per second")
- block = peripheral.wrap("left")
- for i = 1,Length do--loop through ticks
- if song[i] then--if there are notes on there then loop through all of them
- --print(i.." is "..#song[i])
- for j = 1,#song[i] do--loop through the notes
- --print(song[i][j][2])
- block.setCommand("/playsound "..song[i][j][1].." @a ~ ~ ~ "..song[i][j][3].." "..song[i][j][2].." 0")
- block.runCommand()
- end
- end
- --sleep(0.05)
- progress(i)
- sleep(1/(tempo/100))
- end
- term.setCursorPos(1,15)
- file:close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement