Advertisement
Alakazard12

Columna1's NBS Player

Sep 27th, 2014
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.01 KB | None | 0 0
  1. term.clear()
  2. term.setCursorPos(1,1)
  3.  
  4. filename = "Songs/Vanilla Twilight.nbs"
  5. nb = {}
  6. song = {}
  7.  
  8. insts = {"note.harp","note.bassattack","note.bd","note.snare","note.hat"}
  9. volume = {1,           0.5,              0.4,       0.3,         0.3}
  10.  
  11. for i = 0,24 do
  12.     nb[i] = 0.5*(2^(i/12))
  13. end
  14.  
  15. notes = {}
  16. file = fs.open(filename,"rb")
  17. if not file then error("Failed to open file") end
  18. function readByte()
  19.     return file.read()
  20. end
  21. function bytes_to_int(a,b,c,d)--little Endian
  22.     sum = 0
  23.     sum = sum + a
  24.     sum = sum + b * 2 ^ 8
  25.     sum = sum + c * 2 ^ 16
  26.     sum = sum + d * 2 ^ 24
  27.     return sum
  28. end
  29.  
  30. function readInt()
  31.     e = readByte()
  32.     f = readByte()
  33.     g = readByte()
  34.     h = readByte()
  35.     return bytes_to_int(e,f,g,h)
  36. end
  37.  
  38. function readShort()
  39.     g = readByte()
  40.     h = readByte()
  41.     e = 0
  42.     f = 0
  43.     return bytes_to_int(g,h,e,f)
  44. end
  45. function readString()
  46.     str = ""
  47.     length = readInt()
  48.     for i = 1,length do
  49.         str = str..string.char(readByte())
  50.     end
  51.     return str
  52. end
  53.  
  54. function readHeader()
  55.     Length = readShort()
  56.     height = readShort()
  57.     name = readString()
  58.     author = readString()
  59.     originalAuthor = readString()
  60.     description = readString()
  61.     tempo = readShort()
  62.     autoSave = readByte()
  63.     autoSaveDuration = readByte()
  64.     timeSig = readByte()
  65.     minutesSpent = readInt()
  66.     LeftClicks = readInt()
  67.     rightClicks = readInt()
  68.     blocksAdded = readInt()
  69.     blocksRemoved = readInt()
  70.     OrigionalFile = readString()
  71. end
  72.  
  73. function addNoteBlock(tick, layer, instrument, key)
  74.     if not song[tick] then
  75.         song[tick] = {}
  76.         song[tick][1] = {insts[instrument+1],nb[key-33],volume[instrument+1]}
  77.     else
  78.     --print(#song[tick]+1)
  79.     song[tick][#song[tick]+1] = {insts[instrument+1],nb[key-33],volume[instrument+1]}
  80.     end
  81. end
  82.  
  83. function readSong()
  84.     tick = -1
  85.     jumps = 0
  86.     while true do
  87.         jumps = readShort()
  88.         if jumps == 0 then
  89.             break
  90.         end
  91.         tick = tick + jumps
  92.         layer = -1
  93.         while true do
  94.             jumps = readShort()
  95.             if jumps == 0 then
  96.                 break
  97.             end
  98.             layer = layer + jumps
  99.             inst = readByte()
  100.             key = readByte()
  101.             addNoteBlock(tick, layer, inst, key)
  102.         end
  103.     end
  104. end
  105.  
  106. function progress(p)
  107.     --51 pixels
  108.     count = 0
  109.     --line 5
  110.     term.setCursorPos(1,19)
  111.     write(math.floor((p/Length)*100).."% Done")
  112.     term.setCursorPos(1,18)
  113.     percent = p/Length
  114.     write("<"..string.rep("=",math.floor(49*percent))..string.rep("-",math.ceil(49-(49*percent)))..">")
  115. end
  116.  
  117. readHeader()
  118. readSong()
  119. print(name)
  120. print(author)
  121. print(description)
  122. print((tempo/100).." Ticks per second")
  123.  
  124. block = peripheral.wrap("left")
  125. for i = 1,Length do--loop through ticks
  126.     if song[i] then--if there are notes on there then loop through all of them
  127.         --print(i.." is "..#song[i])
  128.         for j = 1,#song[i] do--loop through the notes
  129.             --print(song[i][j][2])
  130.             block.setCommand("/playsound "..song[i][j][1].." @a ~ ~ ~ "..song[i][j][3].." "..song[i][j][2].." 0")
  131.             block.runCommand()
  132.         end
  133.     end
  134.     --sleep(0.05)
  135.     progress(i)
  136.     sleep(1/(tempo/100))
  137. end
  138. term.setCursorPos(1,15)
  139. file:close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement