-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDFPWM.java
190 lines (170 loc) · 4.26 KB
/
DFPWM.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
DFPWM1a implementation in Java
operates on 8-bit signed PCM data and little-endian DFPWM data
by Ben "GreaseMonkey" Russell, 2013, 2016 - Public Domain
NOTE, len is in bytes relative to DFPWM (len*8 PCM bytes)
also the main() function takes unsigned 8-bit data and converts it to suit
*/
public class DFPWM
{
private final int RESP_INC;
private final int RESP_DEC;
private final int RESP_PREC;
private final int LPF_STRENGTH;
private int response = 0;
private int level = 0;
private boolean lastbit = false;
private int flastlevel = 0;
private int lpflevel = 0;
private final boolean dfpwm_old;
public DFPWM(boolean newdfpwm) {
dfpwm_old = !newdfpwm;
if (newdfpwm) {
RESP_INC = 1;
RESP_DEC = 1;
RESP_PREC = 10;
LPF_STRENGTH = 140;
} else {
RESP_INC = 7;
RESP_DEC = 20;
RESP_PREC = 8;
LPF_STRENGTH = 100;
}
}
private void ctx_update(boolean curbit)
{
int target = (curbit ? 127 : -128);
int nlevel = (level + ((response*(target - level)
+ (1<<(RESP_PREC-1)))>>RESP_PREC));
if(nlevel == level && level != target)
nlevel += (curbit ? 1 : -1);
int rtarget, rdelta;
if(curbit == lastbit)
{
rtarget = (1<<RESP_PREC)-1;
rdelta = RESP_INC;
} else {
rtarget = 0;
rdelta = RESP_DEC;
}
int nresponse = response + (dfpwm_old ? ((rdelta * (rtarget - response) + 128)>>8) : 0);
if(nresponse == response && response != rtarget)
nresponse += (curbit == lastbit ? 1 : -1);
if(RESP_PREC > 8)
{
if(nresponse < (2<<(RESP_PREC-8)))
nresponse = (2<<(RESP_PREC-8));
}
response = nresponse;
lastbit = curbit;
level = nlevel;
}
public void decompress(byte[] dest, byte[] src, int destoffs, int srcoffs, int len)
{
for(int i = 0; i < len; i++)
{
byte d = src[srcoffs++];
for(int j = 0; j < 8; j++)
{
// apply context
boolean curbit = ((d&1) != 0);
boolean lastbit = this.lastbit;
ctx_update(curbit);
d >>= 1;
// apply noise shaping
int blevel = (byte)(curbit == lastbit
? level
: ((flastlevel + level + 1)>>1));
flastlevel = level;
// apply low-pass filter
lpflevel += ((LPF_STRENGTH * (blevel - lpflevel) + 0x80)>>8);
dest[destoffs++] = (byte)(lpflevel);
}
}
}
public void compress(byte[] dest, byte[] src, int destoffs, int srcoffs, int len)
{
for(int i = 0; i < len; i++)
{
int d = 0;
for(int j = 0; j < 8; j++)
{
int inlevel = src[srcoffs++];
boolean curbit = (inlevel > level || (inlevel == level && level == 127));
d = (curbit ? (d>>1)+128 : d>>1);
ctx_update(curbit);
}
dest[destoffs++] = (byte)d;
}
}
public static void main(String[] args) throws Exception // FUCK THE POLICE
{
int mode = 0;
boolean newcodec = true;
for(int i = 0; i < args.length; i++)
{
String arg = args[i];
if(arg.equals("-e"))
mode = 1;
else if(arg.equals("-d"))
mode = 2;
else if(arg.equals("-o"))
newcodec = false;
}
byte[] pcmin = new byte[1024];
byte[] pcmout = new byte[1024];
byte[] cmpdata = new byte[128];
DFPWM incodec = new DFPWM(newcodec);
DFPWM outcodec = new DFPWM(newcodec);
int ctr;
if(mode == 0)
{
do
{
for(ctr = 0; ctr < 1024;)
{
int amt = System.in.read(pcmin, ctr, 1024-ctr);
if(amt == -1) break;
ctr += amt;
}
ctr &= ~0x07;
for(int i = 0; i < ctr; i++)
pcmin[i] ^= (byte)0x80;
incodec.compress(cmpdata, pcmin, 0, 0, ctr/8);
outcodec.decompress(pcmout, cmpdata, 0, 0, ctr/8);
for(int i = 0; i < ctr; i++)
pcmout[i] ^= (byte)0x80;
System.out.write(pcmout, 0, ctr);
} while(ctr == 1024);
} else if(mode == 1) {
do
{
for(ctr = 0; ctr < 1024;)
{
int amt = System.in.read(pcmin, ctr, 1024-ctr);
if(amt == -1) break;
ctr += amt;
}
ctr &= ~0x07;
for(int i = 0; i < ctr; i++)
pcmin[i] ^= (byte)0x80;
incodec.compress(cmpdata, pcmin, 0, 0, ctr/8);
System.out.write(cmpdata, 0, ctr/8);
} while(ctr == 1024);
} else if(mode == 2) {
do
{
for(ctr = 0; ctr < 128;)
{
int amt = System.in.read(cmpdata, ctr, 128-ctr);
if(amt == -1) break;
ctr += amt;
}
outcodec.decompress(pcmout, cmpdata, 0, 0, ctr);
for(int i = 0; i < ctr*8; i++)
pcmout[i] ^= (byte)0x80;
System.out.write(pcmout, 0, ctr*8);
} while(ctr == 128);
}
}
}