ecco7777

CC NBS player

Nov 25th, 2025
644
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.78 KB | None | 0 0
  1. -- simple_nbs_player.lua
  2. -- Minimal NBS player for CraftOS 1.7 + OpenPeripheral note blocks
  3.  
  4. local nb = peripheral.wrap("right") -- change side to your note block
  5. if not nb then
  6.     print("No note block peripheral found!")
  7.     return
  8. end
  9.  
  10. -- Load .nbs file (binary)
  11. local function loadNBS(filename)
  12.     local f = fs.open(filename, "rb")
  13.     if not f then return nil end
  14.     local data = f.readAll()
  15.     f.close()
  16.     return data
  17. end
  18.  
  19. -- Convert bytes to little-endian 16-bit integer
  20. local function readShort(data, pos)
  21.     local a = string.byte(data, pos)
  22.     local b = string.byte(data, pos+1)
  23.     return a + b*256
  24. end
  25.  
  26. -- Simple parsing for minimal NBS (version 3–5)
  27. local function parseNBS(data)
  28.     local pos = 8 -- skip header (short length + version info)
  29.     local tempo = readShort(data, pos) / 100 -- ticks per beat
  30.     pos = pos + 2
  31.  
  32.     local notes = {} -- {tick = {{layer, instrument, key}, ...}}
  33.  
  34.     local tick = 0
  35.     while pos < #data do
  36.         local jumpTicks = readShort(data, pos)
  37.         pos = pos + 2
  38.         if jumpTicks == 0 then break end
  39.         tick = tick + jumpTicks
  40.  
  41.         local layer = 0
  42.         while true do
  43.             local jumpLayers = readShort(data, pos)
  44.             pos = pos + 2
  45.             if jumpLayers == 0 then break end
  46.             layer = layer + jumpLayers
  47.  
  48.             local instrument = string.byte(data, pos)
  49.             local key = string.byte(data, pos+1)
  50.             pos = pos + 2
  51.  
  52.             if not notes[tick] then notes[tick] = {} end
  53.             table.insert(notes[tick], {layer=layer, instrument=instrument, key=key})
  54.         end
  55.     end
  56.  
  57.     return notes, tempo
  58. end
  59.  
  60. -- Map NBS instruments to Minecraft sounds
  61. local instrumentMap = {
  62.     [0] = "note.harp",
  63.     [1] = "note.bass",
  64.     [2] = "note.snare",
  65.     [3] = "note.hat",
  66.     [4] = "note.bassattack"
  67. }
  68.  
  69. -- Convert NBS key to pitch (rough approximation)
  70. local function keyToPitch(key)
  71.     return 2 ^ ((key - 45) / 12)
  72. end
  73.  
  74. -- Play parsed notes
  75. local function playNBS(notes, tempo)
  76.     local sortedTicks = {}
  77.     for tick in pairs(notes) do table.insert(sortedTicks, tick) end
  78.     table.sort(sortedTicks)
  79.  
  80.     for _, tick in ipairs(sortedTicks) do
  81.         local tickNotes = notes[tick]
  82.         for _, n in ipairs(tickNotes) do
  83.             local sound = instrumentMap[n.instrument] or "note.harp"
  84.             local pitch = keyToPitch(n.key)
  85.             nb.playSound(sound, 1, pitch)
  86.         end
  87.         sleep(tempo) -- wait until next tick
  88.     end
  89. end
  90.  
  91. -- ===== Main =====
  92. local filename = "song.nbs"
  93. local data = loadNBS(filename)
  94. if not data then
  95.     print("Failed to load "..filename)
  96.     return
  97. end
  98.  
  99. local notes, tempo = parseNBS(data)
  100. print("Playing "..filename.." at tempo "..tempo)
  101. playNBS(notes, tempo)
  102. print("Done!")
  103.  
Advertisement
Add Comment
Please, Sign In to add comment