Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local FileName = "tet.mid"
- local File = fs.open(FileName, "rb")
- local ToHex = "%X"
- --(( Functions ))--
- -- returns a HEX string
- local function BigEndian(Size, Stri, Conc)
- local Tbl = {}
- for Count = 1, Size do
- table.insert(Tbl, File.read())
- end
- if Stri == true then
- local Str = ""
- for i,v in pairs(Tbl) do
- Str = Str .. string.char(v)
- end
- return Str
- elseif Conc == true then
- return table.concat(Tbl, "")
- else
- return Tbl
- end
- end
- -- returns a HEX string
- local function LittleEndian(Size)
- local T = {}
- for Count = 1,Size do
- table.insert(T,ToHex:format(File.read()))
- end
- local Str = ""
- for Count = #T,1,-1 do
- Str = Str .. T[Count]
- end
- return Str
- end
- local function ToBit(Byte)
- local Tbl = {}
- local Num = {
- 128;
- 64;
- 32;
- 16;
- 8;
- 4;
- 2;
- 1;
- }
- local On = Byte
- for i = 1, 8 do
- local Wh = math.floor(On/Num[i])
- Tbl[i] = Wh
- if Wh == 1 then
- On = On - Num[i]
- end
- end
- return Tbl
- end
- local function parseVarInt(s, bits) -- parses multiple bytes as an integer
- if not s then
- error("error parsing file")
- end
- bits = bits or 8
- local mask = bit.brshift(0xFF, 8 - bits)
- local num = 0
- for i = 1, s:len() do
- num = num + bit.blshift(bit.band(s:byte(i), mask), (s:len() - i) * bits)
- end
- return num
- end
- local function GetVL()
- local Done = ""
- while true do
- local By = File:read()
- Done = Done .. string.char(By)
- local Bits = ToBit(By)
- if Bits[1] == 0 then
- return parseVarInt(Done, 7)
- end
- end
- end
- -- Header Chunk
- local ChunkID = BigEndian(4, true)
- local ChunkSize = tonumber(BigEndian(4, false, true))
- local FormatType = tonumber(BigEndian(2, false, true))
- local NTracks = tonumber(BigEndian(2, false, true))
- local TDiv = BigEndian(2, false)
- -- Track Chunk
- local TChunkID = BigEndian(4, true)
- local TChunkSize = tonumber(BigEndian(4, false, true))
- print("Header Chunk")
- print("Chunk ID: " .. ChunkID)
- print("Chunk Size: " .. ChunkSize)
- print("Format Type: " .. FormatType)
- print("Number of Tracks: " .. NTracks)
- -- print("Time Division: " .. TDiv)
- print("Track Chunk")
- print("Chunk ID: " .. TChunkID)
- print("Chunk Size: " .. TChunkSize)
- local function GetPar(By)
- return bit.band(0xF, By), File:read(), File:read()
- end
- local Tra = {}
- --for Ded = 1, math.huge do
- local Delt = GetVL()
- print("Delta time: " .. Delt)
- File:read()
- local By = File:read()
- local EType = bit.band(0xF0, By)
- print("Event Type: 0x" .. ToHex:format(EType))
- if EType == 0x80 then -- Note Off
- print(":3")
- local Channel, Note, Velocity = GetPar()
- table.insert(Tra, {"note_off", Note, Velocity, Channel})
- elseif EType == 0x9 then -- Note On
- local Channel, Note, Velocity = GetPar()
- table.insert(Tra, {"note_on", Note, Velocity, Channel})
- elseif EType == 0xA then -- Aftertouch
- GetPar()
- elseif EType == 0xB then -- Controller
- GetPar()
- elseif EType == 0xC then -- Program change
- GetPar()
- elseif EType == 0xD then -- Aftertouch
- GetPar()
- elseif EType == 0xE then -- Pitch Bend
- GetPar()
- elseif EType == 0xF0 then
- local Length = GetVL()
- for i = 1, Length - 1 do
- File.read()
- end
- print(Length)
- break
- end
- --end
- File.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement