Barnet

player_nbs.lua

Jun 9th, 2021
545
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local playernbs = {}
  2.  
  3. local loadAsModule = dofile("loadasmodule.lua").loadAsModule
  4. local nbs = loadAsModule("nbs.lua")
  5. local ats = loadAsModule("ats.lua")
  6.  
  7. local NBS_MC_inst = {
  8.   [1] = 0,
  9.   [2] = 4,
  10.   [3] = 1,
  11.   [4] = 2,
  12.   [5] = 3,
  13.   [6] = 5,
  14.   [7] = 6
  15. }
  16.  
  17. local playNBSNote = function(id, subid, inst, note, volume)
  18.   local mcinst = NBS_MC_inst[inst + 1]
  19.   local mcnote = note - 33
  20.   local mcvolume = volume / 200
  21.   os.queueEvent("player:slave", id, subid, mcinst, mcnote, mcvolume)
  22. end
  23.  
  24. local playNBSNotes = function(id, subid, inst, noteTable, tSong)
  25.  
  26.   local i = 0
  27.   local layers = tSong.layers
  28.   -- TODO figure out why ipairs() doesn't work
  29.   for layer,note in pairs(noteTable) do
  30.     i = i + 1
  31.     playNBSNote(id, subid, inst, note + 33, layers[layer].volume)
  32.   end
  33.   for i=1,i do
  34.     repeat
  35.       local e, i, s = os.pullEvent("player:slave") -- wait for slaves
  36.     until i == id and s == subid -- our slaves
  37.   end
  38. end
  39.  
  40. local playNBSInstruments = function(id, subid, instTable, tSong)
  41.  
  42.   os.queueEvent("player:monitor", id, subid, "pre")
  43.   os.pullEvent("player:monitor") -- wait for monitor
  44.  
  45.   -- TODO figure out why ipairs() doesn't work
  46.   for inst, notes in pairs(instTable) do
  47.     playNBSNotes(id, subid, inst - 1, notes, tSong)
  48.   end
  49.  
  50.   os.queueEvent("player:monitor", id, subid, "post")
  51.   os.pullEvent("player:monitor") -- wait for monitor
  52.  
  53. end
  54.  
  55. playernbs.new = function(id, subid, filename, monitorSettings)
  56.   return function()
  57.     local tSong = assert(nbs.load(filename))
  58.  
  59.     local delay = tSong.delay
  60.     local lenght = tSong.lenght
  61.     local tps = 1/delay
  62.  
  63.     monitorSettings.length = lenght / delay
  64.     monitorSettings.footerRight = tps .. " TPS"
  65.  
  66.     local row = 0
  67.  
  68.     local wait = ats.makesleeper()
  69.  
  70.     local startTime = os.clock()
  71.     for _,tick in ipairs(tSong) do
  72.       playNBSInstruments(id, subid, tick, tSong)
  73.       row = row + 1
  74.       wait(delay)
  75.       if rs.getInput("front") then
  76.         return
  77.       end
  78.     end
  79.     local x = os.clock() - startTime
  80.     print("Total time: ", x)
  81.     local avgtps = row / x
  82.     print("Average TPS: ", avgtps)
  83.     local diff = avgtps - tps
  84.     if diff >= 0.05 or diff <= -0.05 then
  85.       print("WARNING")
  86.       print("Song was expected to play at an average TPS of ", tps)
  87.       print("Unless you've changed game tick rate, THIS IS A BUG!")
  88.       print("Report it at https://github.com/SoniEx2/NBSPlayer")
  89.     end
  90.   end
  91. end
  92.  
  93. return playernbs
RAW Paste Data