Advertisement
Alakazard12

mid

Feb 17th, 2014
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. local FileName = "tet.mid"
  2. local File = fs.open(FileName, "rb")
  3. local ToHex = "%X"
  4.  
  5. --(( Functions ))--
  6.  
  7. -- returns a HEX string
  8. local function BigEndian(Size, Stri, Conc)
  9. local Tbl = {}
  10. for Count = 1, Size do
  11. table.insert(Tbl, File.read())
  12. end
  13. if Stri == true then
  14. local Str = ""
  15. for i,v in pairs(Tbl) do
  16. Str = Str .. string.char(v)
  17. end
  18. return Str
  19. elseif Conc == true then
  20. return table.concat(Tbl, "")
  21. else
  22. return Tbl
  23. end
  24. end
  25.  
  26. -- returns a HEX string
  27. local function LittleEndian(Size)
  28. local T = {}
  29. for Count = 1,Size do
  30. table.insert(T,ToHex:format(File.read()))
  31. end
  32. local Str = ""
  33. for Count = #T,1,-1 do
  34. Str = Str .. T[Count]
  35. end
  36. return Str
  37. end
  38.  
  39. local function ToBit(Byte)
  40. local Tbl = {}
  41. local Num = {
  42. 128;
  43. 64;
  44. 32;
  45. 16;
  46. 8;
  47. 4;
  48. 2;
  49. 1;
  50. }
  51. local On = Byte
  52. for i = 1, 8 do
  53. local Wh = math.floor(On/Num[i])
  54. Tbl[i] = Wh
  55. if Wh == 1 then
  56. On = On - Num[i]
  57. end
  58. end
  59.  
  60. return Tbl
  61. end
  62.  
  63. local function parseVarInt(s, bits) -- parses multiple bytes as an integer
  64. if not s then
  65. error("error parsing file")
  66. end
  67. bits = bits or 8
  68. local mask = bit.brshift(0xFF, 8 - bits)
  69. local num = 0
  70. for i = 1, s:len() do
  71. num = num + bit.blshift(bit.band(s:byte(i), mask), (s:len() - i) * bits)
  72. end
  73. return num
  74. end
  75.  
  76. local function GetVL()
  77. local Done = ""
  78. while true do
  79. local By = File:read()
  80. Done = Done .. string.char(By)
  81. local Bits = ToBit(By)
  82. if Bits[1] == 0 then
  83. return parseVarInt(Done, 7)
  84. end
  85. end
  86. end
  87.  
  88. -- Header Chunk
  89. local ChunkID = BigEndian(4, true)
  90. local ChunkSize = tonumber(BigEndian(4, false, true))
  91. local FormatType = tonumber(BigEndian(2, false, true))
  92. local NTracks = tonumber(BigEndian(2, false, true))
  93. local TDiv = BigEndian(2, false)
  94.  
  95. -- Track Chunk
  96. local TChunkID = BigEndian(4, true)
  97. local TChunkSize = tonumber(BigEndian(4, false, true))
  98.  
  99. print("Header Chunk")
  100. print("Chunk ID: " .. ChunkID)
  101. print("Chunk Size: " .. ChunkSize)
  102. print("Format Type: " .. FormatType)
  103. print("Number of Tracks: " .. NTracks)
  104. -- print("Time Division: " .. TDiv)
  105.  
  106. print("Track Chunk")
  107. print("Chunk ID: " .. TChunkID)
  108. print("Chunk Size: " .. TChunkSize)
  109.  
  110. local function GetPar(By)
  111. return bit.band(0xF, By), File:read(), File:read()
  112. end
  113.  
  114. local Tra = {}
  115.  
  116. --for Ded = 1, math.huge do
  117. local Delt = GetVL()
  118. print("Delta time: " .. Delt)
  119. File:read()
  120. local By = File:read()
  121. local EType = bit.band(0xF0, By)
  122. print("Event Type: 0x" .. ToHex:format(EType))
  123. if EType == 0x80 then -- Note Off
  124. print(":3")
  125. local Channel, Note, Velocity = GetPar()
  126. table.insert(Tra, {"note_off", Note, Velocity, Channel})
  127. elseif EType == 0x9 then -- Note On
  128. local Channel, Note, Velocity = GetPar()
  129. table.insert(Tra, {"note_on", Note, Velocity, Channel})
  130. elseif EType == 0xA then -- Aftertouch
  131. GetPar()
  132. elseif EType == 0xB then -- Controller
  133. GetPar()
  134. elseif EType == 0xC then -- Program change
  135. GetPar()
  136. elseif EType == 0xD then -- Aftertouch
  137. GetPar()
  138. elseif EType == 0xE then -- Pitch Bend
  139. GetPar()
  140. elseif EType == 0xF0 then
  141. local Length = GetVL()
  142. for i = 1, Length - 1 do
  143. File.read()
  144. end
  145. print(Length)
  146. break
  147. end
  148. --end
  149.  
  150. File.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement