forked from SonifyIt/sonification
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstreams.pde
executable file
·334 lines (287 loc) · 6.45 KB
/
streams.pde
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// image->raw->audio and audio->raw->image functions
// helper function to pipe filters
public interface Piper {
public float read();
}
class Pipe implements Piper {
int bsize;
float[] buff;
int ridx, widx;
public Pipe(int buffersize) {
bsize = buffersize;
buff = new float[bsize];
ridx = widx = 0;
;
}
void write(float v) {
buff[widx] = v;
widx = (widx+1)%bsize;
}
float read() {
float v = buff[ridx];
ridx = (ridx+1)%bsize;
return v;
}
}
final static float s8 = 1.0/(float)0xff;
final static float s16 = 1.0/(float)0xffff;
final static float s24 = 1.0/(float)0xffffff;
class RawReader implements Piper {
int law, sign, bits, endian;
public ImageStreamReader r;
public RawReader(PImage img, int mode, int law, int sign, int bits, int endian) {
this.law = law;
this.sign = sign;
this.bits = bits;
this.endian = endian;
r = new ImageStreamReader(img, mode);
}
void reset() {
r.reset();
}
float read() {
int b1 = 0;
int b2 = 0;
int b3 = 0;
b1 = r.read();
if (bits == B16 || bits == B24) b2 = r.read();
if (bits == B24) b3 = r.read();
int ires = b1;
if (endian == BIG_ENDIAN && bits != B8) {
ires = (ires<<8) | b2;
if (bits == B24) ires = (ires<<8) | b3;
}
if (endian == LITTLE_ENDIAN && bits != B8) {
ires |= (b2 << 8);
if (bits == B24) ires |= (b3 << 16);
}
float res;
if (sign == SIGNED) {
ires = (ires << (32 - bits) ) >> (32 - bits);
switch(bits) {
case B8:
res = 2.0*(ires*s8);
break;
case B16:
res = 2.0*(ires*s16);
break;
default:
res = 2.0*(ires*s24);
}
}
else {
switch(bits) {
case B8:
res = 2.0*((ires*s8)-0.5);
break;
case B16:
res = 2.0*((ires*s16)-0.5);
break;
default:
res = 2.0*((ires*s24)-0.5);
}
}
switch(law) {
case U_LAW:
res = ulaw(res);
break;
case A_LAW:
res = alaw(res);
break;
}
return constrain(res, -1.0, 1.0);
}
}
class RawWriter {
int law, sign, bits, endian;
public ImageStreamWriter w;
public RawWriter(PImage img, int mode, int law, int sign, int bits, int endian) {
this.law = law;
this.sign = sign;
this.bits = bits;
this.endian = endian;
w = new ImageStreamWriter(img, mode);
}
void reset() {
w.reset();
}
void write(float v) {
float res = v;
switch(law) {
case U_LAW:
res = revulaw(res);
break;
case A_LAW:
res = revalaw(res);
break;
}
int ires=0;
float sh = sign == SIGNED ? 0.0 : 0.5;
switch(bits) {
case B8:
ires = 0xff & (int)floor(0xff*(0.5*res+sh));
break;
case B16:
ires = 0xffff & (int)floor(0xffff*(0.5*res+sh));
break;
default:
ires = 0xffffff & (int)floor(0xffffff*(0.5*res+sh));
}
if (endian == BIG_ENDIAN) {
if (bits == B24) w.write( ires >> 16 );
if (bits == B24 || bits == B16) w.write( (ires & 0xff00) >> 8 );
w.write( ires & 0xff );
}
else {
w.write( ires & 0xff );
if (bits == B24 || bits == B16) w.write( (ires & 0xff00) >> 8 );
if (bits == B24) w.write( ires >> 16 );
}
}
}
static final float A = 87.6;
static final float rA = 1.0/A;
static final float lnA = 1.0+log(A);
static final float rlnA = 1.0/lnA;
float alaw(float x) {
float sgnx = x < 0 ? -1.0 : 1.0;
float absx = abs(x);
if (absx<rA) {
return sgnx*(A*absx)/lnA;
}
else {
return sgnx*(1+log(A*absx))/lnA;
}
}
float revalaw(float x) {
float sgnx = x < 0 ? -1.0 : 1.0;
float absx = abs(x);
if (absx<rlnA) {
return sgnx*(absx*lnA)/A;
}
else {
return sgnx*(exp(absx*lnA-1.0))/A;
}
}
static final float U = 255.0;
static final float rU = 1.0/255.0;
static final float U1 = 1.0 + U;
static final float lnU = log(U1);
float ulaw(float x) {
float sgnx = x < 0 ? -1.0 : 1.0;
return sgnx*log(1.0+U*abs(x))/lnU;
}
float revulaw(float x) {
float sgnx = x < 0 ? -1.0 : 1.0;
return sgnx * rU * (pow(U1, abs(x))-1.0);
}
public class ImageStreamReader {
public PImage rimg;
public boolean taken = false;
private int mode;
public int takencnt=0;
public ImageStreamReader(PImage i, int m) {
rimg = i;
rimg.loadPixels();
mode = m;
}
void convertColorspace(int cs) {
for (int i=0;i<rimg.pixels.length;i++) {
rimg.pixels[i] = toColorspace(rimg.pixels[i],cs);
}
}
void reset() {
ridx = 0;
rstate = 0;
taken = false;
takencnt = 0;
}
int ridx = 0;
int rstate = 0;
int read() {
if (taken) return 0;
int res;
switch(rstate) {
case 0:
res = (rimg.pixels[ridx] & 0xff0000) >> 16;
break;
case 1:
res = (rimg.pixels[ridx] & 0xff00) >> 8;
break;
default:
res = rimg.pixels[ridx] & 0xff;
}
if (mode == PLANAR) {
ridx++;
if (ridx == rimg.pixels.length) {
rstate++;
ridx=0;
if (rstate == 3) taken = true;
}
}
else {
rstate++;
if (rstate == 3) {
ridx++;
rstate = 0;
if (ridx == rimg.pixels.length) taken = true;
}
}
takencnt++;
return res;
}
}
public class ImageStreamWriter {
public PImage wimg;
public boolean written = false;
private int mode;
public ImageStreamWriter(PImage i, int m) {
wimg = i;
wimg.loadPixels();
mode = m;
}
void convertColorspace(int cs) {
for (int i=0;i<wimg.pixels.length;i++) {
wimg.pixels[i] = fromColorspace(wimg.pixels[i],cs);
}
}
void reset() {
widx = 0;
wstate = 0;
written = false;
}
int widx = 0;
int wstate = 0;
void write(int w) {
if (written) return;
switch(wstate) {
case 0:
wimg.pixels[widx] = (wimg.pixels[widx] & 0xff00ffff) | ( (w & 0xff) << 16);
break;
case 1:
wimg.pixels[widx] = (wimg.pixels[widx] & 0xffff00ff) | ( (w & 0xff) << 8);
break;
default:
wimg.pixels[widx] = (wimg.pixels[widx] & 0xffffff00) | (w & 0xff);
}
if (mode == PLANAR) {
widx++;
if (widx == wimg.pixels.length) {
wstate++;
widx=0;
if (wstate == 3) written = true;
}
}
else {
wstate++;
if (wstate == 3) {
widx++;
wstate = 0;
if (widx == wimg.pixels.length) {
written = true;
wimg.updatePixels();
}
}
}
}
}