Advertisement
Alakazard12

conv

Feb 18th, 2014
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.06 KB | None | 0 0
  1. --[By Reububble]--
  2.  
  3. function hex(IN)
  4. K = "0123456789ABCDEF"
  5. out = ""
  6. I = 0
  7. D = nil
  8. while IN>0 do
  9. I = I+1
  10. D = IN%16+1
  11. IN = math.floor(IN/16)
  12. out = string.sub(K,D,D)..out
  13. end
  14. while #out<2 do out="0"..out end
  15. return out
  16. end
  17.  
  18. function VLQ()
  19. ret = 0
  20. red = h.read()
  21. while red>127 do
  22. ret = ret*128 + (red-128)
  23. red = h.read()
  24. end
  25. return ret*128+red
  26. end
  27.  
  28. args = {...}
  29. nom=''
  30.  
  31. for i=1,#args do
  32. nom = i==1 and args[i] or nom.." "..args[i]
  33. end
  34.  
  35. h=fs.open(nom,"rb")
  36.  
  37. term.clear()
  38. term.setCursorPos(1,1)
  39. term.write("reading")
  40.  
  41. for i=1,10 do h.read() end
  42. remaining = h.read()*256+h.read()
  43. division = h.read()*256+h.read()
  44. if division<32768 then
  45. dT=division/4
  46. else
  47. FpS=math.floor((division-32768)/128)
  48. TpF=division-32768-FpS*128
  49. dT=TpF*FpS/4
  50. end
  51.  
  52. tab = {} --format
  53. --{{time,data,data,etc},{etc}}
  54.  
  55. function Track()
  56. os.queueEvent('&',0)
  57. for i=1,8 do h.read() end
  58. Tick = 0 -- Start at tick 0
  59. Jump = VLQ() -- Jump
  60. Tick = Tick + Jump -- Tick increases
  61. Event = hex(h.read()) -- Event
  62. repeat
  63. os.queueEvent('Blah') os.pullEvent('Blah')
  64. Data = h.read() -- Data call 1
  65. status = string.sub(Event,1,1) --First Hex of it
  66. repeat
  67. if status == '8' or status == '9'
  68. or status == 'A' or status == 'B'
  69. or status == 'E' then -- First bit is 1
  70. if status == '9' then
  71. --SONG ON
  72. Volume = h.read()
  73. if Volume>0 then --Not a false alarm
  74. time = math.floor(Tick/dT) --store Tick as time
  75. broken = false
  76. while Data>25 do
  77. Data = Data-24 --put it within the playable range
  78. end
  79. for i=1,#tab do --check all previous entries
  80. if time == tab[i][1] then --for same time
  81. exist = false
  82. for k=2,#tab[i] do
  83. if Data==tab[i][k] then
  84. exist = true --the note is already there
  85. break
  86. end
  87. end
  88. if not exist then
  89. table.insert(tab[i],Data) --add the Data to the end
  90. end
  91. broken = true
  92. break
  93. end
  94. end
  95. if not broken then --first appearance of time
  96. broken = false
  97. for i=1,#tab do --check all entries
  98. if tab[i][1]>time then --for where the time fits
  99. table.insert(tab,i,{time,Data})
  100. broken = true
  101. break
  102. end
  103. end
  104. if not broken then --entry is last
  105. table.insert(tab,{time,Data})
  106. end
  107. end
  108. end
  109. else --not song but has
  110. Data = h.read() --this 2nd Data call
  111. end
  112. elseif status =='F' then
  113. if string.sub(Event,2,2) == 'F' then
  114. Meta = hex(Data) --META event
  115. end
  116. Length = VLQ()
  117. for i=1,Length do
  118. Data = h.read()
  119. end
  120. end
  121. if Meta == "2F" then --finish track
  122. break
  123. end
  124. Jump = VLQ()
  125. Tick = Tick + Jump
  126. Data = h.read()
  127. until Data>127 --quit running status
  128. Event = hex(Data) --Data was actually and event
  129. until Meta == "2F" --track finish
  130. Meta = nil
  131. end
  132.  
  133. repeat
  134. Track()
  135. remaining = remaining-1 --only do number of tracks
  136. until remaining<1
  137.  
  138. string.gsub(nom,' ','_')
  139.  
  140. h.close()
  141. h=fs.open(string.sub(nom,1,#nom-4),"wb")
  142.  
  143. sleep(10)
  144.  
  145. term.setCursorPos(1,1)
  146. term.clear()
  147. term.write("writing")
  148.  
  149. os.loadAPI('progress')
  150.  
  151. function write()
  152. for i=1,#tab do
  153. T = i==1
  154. and math.floor(tab[i][1])
  155. or math.floor((tab[i][1]-tab[i-1][1]))
  156. n=math.log(T)/math.log(128)
  157. --n is the number of MSBs of 1
  158. if n>0 then
  159. for j=1,n do
  160. T,a=math.modf(T/128)
  161. h.write(128*(1+a))
  162. end
  163. end
  164. if T%100==0 then sleep(0.01) end
  165. h.write(T)
  166. for j=2,#tab[i] do
  167. h.write(tab[i][j]-1)
  168. end
  169. if i==#tab then
  170. h.write(127) --end of whole thing
  171. else
  172. h.write(126) --end of just this
  173. end
  174. os.queueEvent("%",tab[i][1]/tab[#tab][1])
  175. os.queueEvent('Blah') os.pullEvent('Blah')
  176. end
  177. os.queueEvent("%",1)
  178. end
  179.  
  180. parallel.waitForAll(write)
  181. h.close()
  182. os.unloadAPI("progress")
  183. term.setCursorPos(1,1)
  184. term.setBackgroundColor(2^15)
  185. term.clear()
  186. term.setTextColor(1)
  187. print("Conversion Finished")
  188. print("File saved as "..string.sub(nom,1,#nom-4))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement