Advertisement
HappySunChild

NBS-Player_Test-OpenComputers

Aug 7th, 2022 (edited)
804
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.84 KB | None | 0 0
  1. local component = require("component")
  2. local fs = require("filesystem")
  3. local shell = require("shell")
  4. local computer = require("computer")
  5.  
  6. local song = {}
  7.  
  8. function getByte(f)
  9.     return string.byte(f:read(1))
  10. end
  11.  
  12. function readString(file)
  13.     local strln = get_int_32(getByte(file),getByte(file),getByte(file),getByte(file))
  14.     local str = ""
  15.     str = file:read(strln)
  16.     return str
  17. end
  18.  
  19. function get_int_16(b1, b2)
  20.     local n = b1 + 256 * b2
  21.     n = (n > 32767) and (n - 32768) or n
  22.     return n
  23. end  
  24.  
  25. function get_int_32(b1, b2, b3, b4)
  26.     if not b4 then error("need four bytes to convert to int",2) end
  27.     local n = b1 + b2*256 + b3*65536 + b4*16777216
  28.     n = (n > 2147483647) and (n - 4294967296) or n
  29.     return n
  30. end
  31.  
  32. function song:loadSong(path)
  33.     if not fs.exists(path) then error("File \"" .. path .. "\" does not exist.\n") end
  34.  
  35.     local file = io.open(path,"rb")
  36.  
  37.     self.length = get_int_16(getByte(file),getByte(file))
  38.     self.height = get_int_16(getByte(file),getByte(file))
  39.     self.name = readString(file)
  40.     self.author = readString(file)
  41.     self.org_author = readString(file)
  42.     self.description = readString(file)
  43.  
  44.     self.tempo = get_int_16(getByte(file),getByte(file)) * 0.01
  45.  
  46.     print("Name: " .. self.name)
  47.     print("Description: " .. self.description)
  48.     print("Length in ticks: " .. self.length)
  49.  
  50.     self.song_ticks = {}
  51.  
  52.     local tick = -1
  53.     local jumps = 0
  54.  
  55.     local counter = 1
  56.  
  57.     while true do
  58.         jumps = get_int_16(getByte(file),getByte(file))
  59.  
  60.         print("Jumps: " .. jumps)
  61.  
  62.         if jumps == 0 then
  63.             break
  64.         end
  65.  
  66.         tick = tick + jumps
  67.  
  68.         local layer = -1
  69.  
  70.         while true do
  71.             self.song_ticks[counter] = {}
  72.  
  73.             jumps = get_int_16(getByte(file),getByte(file))
  74.             if jumps == 0 then
  75.                 break
  76.             end
  77.             layer = layer + jumps
  78.             self.song_ticks[counter].instrument = getByte(file)
  79.             self.song_ticks[counter].note = getByte(file) - 33
  80.             self.song_ticks[counter].layer = layer
  81.             self.song_ticks[counter].tick = tick
  82.            
  83.             print("cticks: " .. self.song_ticks[counter].tick)
  84.  
  85.             counter = counter + 1
  86.         end
  87.     end
  88.  
  89.     file:close()
  90. end
  91.  
  92. function song:play_note(cur_tick, position)
  93.     while true do
  94.         if self.song_ticks[position].tick == cur_tick then
  95.             computer.beep(self.song_ticks[position].note * 100)
  96.         else
  97.             break
  98.         end
  99.     end
  100. end
  101.  
  102. print("started loading")
  103.  
  104. song:loadSong(...)
  105.  
  106. print("finished loading")
  107.  
  108. local finished = false
  109. local current_tick = 0
  110.  
  111. while not finished do
  112.     current_tick = current_tick + 1
  113.  
  114.     os.sleep(1/song.tempo)
  115.     if current_tick == song.length then
  116.         finished = true
  117.     end
  118. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement