Advertisement
fuxoft

Last V8 SID file melody finder

Feb 13th, 2017
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.06 KB | None | 0 0
  1. #!/usr/bin/env luajit
  2.  
  3. local function main()
  4.     local fd = assert(io.open("Last_V8.sid"))
  5.     local d={}
  6.     repeat
  7.         local char = fd:read(1)
  8.         if char then
  9.             table.insert(d,string.byte(char))
  10.         end
  11.     until not char
  12.     fd:close()
  13.     print(#d.." bytes read")
  14.  
  15.     local notes = {[0]="C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "H"}
  16.  
  17.     for i = 1, #d - 3 do
  18.         local result = nil
  19.         if d[i] == 255 and d[i+1] ~= 255 then
  20.             result = {"@"..bit.tohex(i-1)..":"}
  21.             j = i + 1
  22.             local done = nil
  23.             repeat
  24.                 local len = bit.band(d[j], 0x9f)
  25.                 if len > 127 then
  26.                     len = len - 128
  27.                     j=j+1
  28.                 end
  29.                 j=j+1
  30.                 local note = d[j]
  31.                 j=j+1
  32.                 if note < 100 and note > 1 then
  33.                     local oct = math.floor(note / 12)
  34.                     note = note - oct * 12
  35.                     table.insert(result, notes[note]..oct..","..len + 1)
  36.                 else
  37.                     done = true
  38.                     result = nil
  39.                 end
  40.             until done or d[j] == 255
  41.             if result and #result > 4 then
  42.                 table.insert(result, 2, "("..(#result - 1)..")")
  43.                 print(table.concat(result,"  "))
  44.             end
  45.         end
  46.     end
  47. end
  48.  
  49. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement