Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[By Reububble]--
- function hex(IN)
- K = "0123456789ABCDEF"
- out = ""
- I = 0
- D = nil
- while IN>0 do
- I = I+1
- D = IN%16+1
- IN = math.floor(IN/16)
- out = string.sub(K,D,D)..out
- end
- while #out<2 do out="0"..out end
- return out
- end
- function VLQ()
- ret = 0
- red = h.read()
- while red>127 do
- ret = ret*128 + (red-128)
- red = h.read()
- end
- return ret*128+red
- end
- args = {...}
- nom=''
- for i=1,#args do
- nom = i==1 and args[i] or nom.." "..args[i]
- end
- h=fs.open(nom,"rb")
- term.clear()
- term.setCursorPos(1,1)
- term.write("reading")
- for i=1,10 do h.read() end
- remaining = h.read()*256+h.read()
- division = h.read()*256+h.read()
- if division<32768 then
- dT=division/4
- else
- FpS=math.floor((division-32768)/128)
- TpF=division-32768-FpS*128
- dT=TpF*FpS/4
- end
- tab = {} --format
- --{{time,data,data,etc},{etc}}
- function Track()
- os.queueEvent('&',0)
- for i=1,8 do h.read() end
- Tick = 0 -- Start at tick 0
- Jump = VLQ() -- Jump
- Tick = Tick + Jump -- Tick increases
- Event = hex(h.read()) -- Event
- repeat
- os.queueEvent('Blah') os.pullEvent('Blah')
- Data = h.read() -- Data call 1
- status = string.sub(Event,1,1) --First Hex of it
- repeat
- if status == '8' or status == '9'
- or status == 'A' or status == 'B'
- or status == 'E' then -- First bit is 1
- if status == '9' then
- --SONG ON
- Volume = h.read()
- if Volume>0 then --Not a false alarm
- time = math.floor(Tick/dT) --store Tick as time
- broken = false
- while Data>25 do
- Data = Data-24 --put it within the playable range
- end
- for i=1,#tab do --check all previous entries
- if time == tab[i][1] then --for same time
- exist = false
- for k=2,#tab[i] do
- if Data==tab[i][k] then
- exist = true --the note is already there
- break
- end
- end
- if not exist then
- table.insert(tab[i],Data) --add the Data to the end
- end
- broken = true
- break
- end
- end
- if not broken then --first appearance of time
- broken = false
- for i=1,#tab do --check all entries
- if tab[i][1]>time then --for where the time fits
- table.insert(tab,i,{time,Data})
- broken = true
- break
- end
- end
- if not broken then --entry is last
- table.insert(tab,{time,Data})
- end
- end
- end
- else --not song but has
- Data = h.read() --this 2nd Data call
- end
- elseif status =='F' then
- if string.sub(Event,2,2) == 'F' then
- Meta = hex(Data) --META event
- end
- Length = VLQ()
- for i=1,Length do
- Data = h.read()
- end
- end
- if Meta == "2F" then --finish track
- break
- end
- Jump = VLQ()
- Tick = Tick + Jump
- Data = h.read()
- until Data>127 --quit running status
- Event = hex(Data) --Data was actually and event
- until Meta == "2F" --track finish
- Meta = nil
- end
- repeat
- Track()
- remaining = remaining-1 --only do number of tracks
- until remaining<1
- string.gsub(nom,' ','_')
- h.close()
- h=fs.open(string.sub(nom,1,#nom-4),"wb")
- sleep(10)
- term.setCursorPos(1,1)
- term.clear()
- term.write("writing")
- os.loadAPI('progress')
- function write()
- for i=1,#tab do
- T = i==1
- and math.floor(tab[i][1])
- or math.floor((tab[i][1]-tab[i-1][1]))
- n=math.log(T)/math.log(128)
- --n is the number of MSBs of 1
- if n>0 then
- for j=1,n do
- T,a=math.modf(T/128)
- h.write(128*(1+a))
- end
- end
- if T%100==0 then sleep(0.01) end
- h.write(T)
- for j=2,#tab[i] do
- h.write(tab[i][j]-1)
- end
- if i==#tab then
- h.write(127) --end of whole thing
- else
- h.write(126) --end of just this
- end
- os.queueEvent("%",tab[i][1]/tab[#tab][1])
- os.queueEvent('Blah') os.pullEvent('Blah')
- end
- os.queueEvent("%",1)
- end
- parallel.waitForAll(write)
- h.close()
- os.unloadAPI("progress")
- term.setCursorPos(1,1)
- term.setBackgroundColor(2^15)
- term.clear()
- term.setTextColor(1)
- print("Conversion Finished")
- print("File saved as "..string.sub(nom,1,#nom-4))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement