|
| 1 | +local function ctx_update(state, curbit) |
| 2 | + local target=(curbit and 127 or -128) |
| 3 | + local nlevel=state.level+math.floor((state.response*(target-state.level)+state.RESP_PREC_HALF)/state.RESP_PREC_POWER) |
| 4 | + if nlevel == state.level and state.level ~= target then |
| 5 | + nlevel=nlevel+(curbit and 1 or -1) |
| 6 | + end |
| 7 | + |
| 8 | + local rtarget, rdelta |
| 9 | + if curbit == state.lastbit then |
| 10 | + rtarget=state.RESP_PREC_MAX |
| 11 | + rdelta=state.RESP_INC |
| 12 | + else |
| 13 | + rtarget=0 |
| 14 | + rdelta=state.RESP_DEC |
| 15 | + end |
| 16 | + |
| 17 | + local nresponse=state.response+(state.new and 0 or math.floor((rdelta*(rtarget-state.response))/256)) |
| 18 | + if nresponse == state.response and state.response ~= rtarget then |
| 19 | + nresponse=nresponse+((curbit == state.lastbit) and 1 or -1) |
| 20 | + end |
| 21 | + |
| 22 | + if state.RESP_PREC > 8 then |
| 23 | + if nresponse < state.RESP_PREC_8 then |
| 24 | + nresponse=state.RESP_PREC_8 |
| 25 | + end |
| 26 | + end |
| 27 | + |
| 28 | + state.response=nresponse |
| 29 | + state.lastbit=curbit |
| 30 | + state.level=nlevel |
| 31 | +end |
| 32 | + |
| 33 | +local function decompress(state, src) |
| 34 | + local dest={} |
| 35 | + for i=1, #src do |
| 36 | + local d=src[i] |
| 37 | + for j=0, 7 do |
| 38 | + -- apply context |
| 39 | + local curbit=(bit32.band(d, bit32.lshift(1, j)) ~= 0) |
| 40 | + local lastbit=state.lastbit |
| 41 | + ctx_update(state, curbit) |
| 42 | + |
| 43 | + -- apply noise shaping |
| 44 | + local blevel = bit32.band((curbit == lastbit) and state.level or math.floor((state.flastlevel+state.level+1)/2), 0xFF) |
| 45 | + if blevel >= 128 then |
| 46 | + blevel=blevel-256 |
| 47 | + end |
| 48 | + state.flastlevel=state.level |
| 49 | + |
| 50 | + -- apply low-pass filter |
| 51 | + state.lpflevel=state.lpflevel+math.floor((state.LPF_STRENGTH*(blevel-state.lpflevel)+0x80)/256) |
| 52 | + dest[#dest+1]=bit32.band(state.lpflevel, 0xFF) |
| 53 | + end |
| 54 | + end |
| 55 | + return dest |
| 56 | +end |
| 57 | + |
| 58 | +local function compress(state, src) |
| 59 | + local dest={} |
| 60 | + for i=1, #src, 8 do |
| 61 | + local d=0 |
| 62 | + for j=0, 7 do |
| 63 | + local inlevel=(src[i+j] or 0) |
| 64 | + if inlevel >= 128 then |
| 65 | + inlevel=inlevel-256 |
| 66 | + end |
| 67 | + local curbit=(inlevel > state.level or (inlevel == state.level and state.level == 127)) |
| 68 | + d=bit32.bor(d/2, curbit and 128 or 0) |
| 69 | + ctx_update(state, curbit) |
| 70 | + end |
| 71 | + dest[#dest+1]=d |
| 72 | + end |
| 73 | + return dest |
| 74 | +end |
| 75 | + |
| 76 | +local dfpwm={} |
| 77 | + |
| 78 | +function dfpwm.new(newdfpwm) |
| 79 | + local state={ |
| 80 | + new=newdfpwm, |
| 81 | + response=0, |
| 82 | + level=0, |
| 83 | + lastbit=false, |
| 84 | + flastlevel=0, |
| 85 | + lpflevel=0, |
| 86 | + decompress=decompress, |
| 87 | + compress=compress |
| 88 | + } |
| 89 | + if newdfpwm then |
| 90 | + state.RESP_INC=1 |
| 91 | + state.RESP_DEC=1 |
| 92 | + state.RESP_PREC=10 |
| 93 | + state.LPF_STRENGTH=140 |
| 94 | + else |
| 95 | + state.RESP_INC=7 |
| 96 | + state.RESP_DEC=20 |
| 97 | + state.RESP_PREC=8 |
| 98 | + state.LPF_STRENGTH=100 |
| 99 | + end |
| 100 | + -- precompute and cache some stuff |
| 101 | + state.RESP_PREC_HALF=bit32.lshift(1, state.RESP_PREC-1) |
| 102 | + state.RESP_PREC_POWER=2^state.RESP_PREC |
| 103 | + state.RESP_PREC_MAX=bit32.lshift(1, state.RESP_PREC)-1 |
| 104 | + state.RESP_PREC_8=bit32.lshift(2, state.RESP_PREC-8) |
| 105 | + return state |
| 106 | +end |
| 107 | + |
| 108 | +return dfpwm |
0 commit comments