SHARE
TWEET

DPCM mixer

HertzDevil Jun 11th, 2015 (edited) 51 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local loop = false
  2.  
  3. function bit(x, d)
  4.   return (x >> d) & 1
  5. end
  6.  
  7. if #arg < 2 then print("Usage: lua dpcm.lua [sample1] [sample2] [output]") os.exit() end
  8. input = {io.open(arg[1], "rb"), io.open(arg[2], "rb")}
  9. if not input[1] then print(arg[1] .. " not found") os.exit() end
  10. if not input[2] then print(arg[2] .. " not found") os.exit() end
  11. output = io.open(arg[3] or "out.dmc", "wb")
  12. if input[1]:seek("end") < input[2]:seek("end") then input[1], input[2] = input[2], input[1] end
  13. input[1]:seek("set", 0)
  14. input[2]:seek("set", 0)
  15.  
  16. local alt = false
  17. while true do
  18.   local a = input[1]:read(1)
  19.   if not a then break end
  20.   a = string.byte(a)
  21.  
  22.   local b = input[2]:read(1)
  23.   if b then
  24.     b = string.byte(b)
  25.   elseif loop then
  26.     input[2]:seek("set", 0)
  27.     b = string.byte(input[2]:read(1))
  28.   else
  29.     b = 0xAA
  30.   end
  31.  
  32.   local out = 0
  33.   if avg then
  34.     for i = 0, 7 do
  35.       local delta = (bit(a, i) + bit(b, i) - 1) / 2
  36.      
  37.     end
  38.   else
  39.     for i = 0, 6, 2 do
  40.       local delta = bit(a, i) + bit(a, i + 1) + bit(b, i) + bit(b, i + 1) - 2
  41.       if delta >= 0 then out = out + (1 << i) end
  42.       if delta > 0 then out = out + (1 << i + 1) end
  43.     end
  44.   end
  45.   output:write(string.char(out))
  46. end
  47.  
  48. input[1]:close()
  49. input[2]:close()
  50. output:close()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top