Barnet

player_nbs.lua

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