Use the new layout code
[SugarCubes.git] / MarkSlee.pde
1 class SpaceTime extends SCPattern {
2
3 SinLFO pos = new SinLFO(0, 15, 3000);
4 SinLFO rate = new SinLFO(1000, 9000, 13000);
5 SinLFO falloff = new SinLFO(10, 70, 5000);
6 float angle = 0;
7
8 BasicParameter rateParameter = new BasicParameter("RATE", 0.5);
9 BasicParameter sizeParameter = new BasicParameter("SIZE", 0.5);
10
11 public SpaceTime(GLucose glucose) {
12 super(glucose);
13 addModulator(pos).trigger();
14 addModulator(rate).trigger();
15 addModulator(falloff).trigger();
16 pos.modulateDurationBy(rate);
17 addParameter(rateParameter);
18 addParameter(sizeParameter);
19 }
20
21 public void onParameterChanged(LXParameter parameter) {
22 if (parameter == rateParameter) {
23 rate.stop().setValue(9000 - 8000*parameter.getValuef());
24 } else if (parameter == sizeParameter) {
25 falloff.stop().setValue(70 - 60*parameter.getValuef());
26 }
27 }
28
29 void run(int deltaMs) {
30 angle += deltaMs * 0.0007;
31 float sVal1 = model.strips.size() * (0.5 + 0.5*sin(angle));
32 float sVal2 = model.strips.size() * (0.5 + 0.5*cos(angle));
33
34 float pVal = pos.getValuef();
35 float fVal = falloff.getValuef();
36
37 int s = 0;
38 for (Strip strip : model.strips) {
39 int i = 0;
40 for (Point p : strip.points) {
41 colors[p.index] = color(
42 (lx.getBaseHuef() + 360 - p.fx*.2 + p.fy * .3) % 360,
43 constrain(.4 * min(abs(s - sVal1), abs(s - sVal2)), 20, 100),
44 max(0, 100 - fVal*abs(i - pVal))
45 );
46 ++i;
47 }
48 ++s;
49 }
50 }
51 }
52
53 class Swarm extends SCPattern {
54
55 SawLFO offset = new SawLFO(0, 16, 1000);
56 SinLFO rate = new SinLFO(350, 1200, 63000);
57 SinLFO falloff = new SinLFO(15, 50, 17000);
58 SinLFO fX = new SinLFO(0, model.xMax, 19000);
59 SinLFO fY = new SinLFO(0, model.yMax, 11000);
60 SinLFO hOffX = new SinLFO(0, model.xMax, 13000);
61
62 public Swarm(GLucose glucose) {
63 super(glucose);
64 addModulator(offset).trigger();
65 addModulator(rate).trigger();
66 addModulator(falloff).trigger();
67 addModulator(fX).trigger();
68 addModulator(fY).trigger();
69 addModulator(hOffX).trigger();
70 offset.modulateDurationBy(rate);
71 }
72
73 float modDist(float v1, float v2, float mod) {
74 v1 = v1 % mod;
75 v2 = v2 % mod;
76 if (v2 > v1) {
77 return min(v2-v1, v1+mod-v2);
78 }
79 else {
80 return min(v1-v2, v2+mod-v1);
81 }
82 }
83
84 void run(int deltaMs) {
85 float s = 0;
86 for (Strip strip : model.strips) {
87 int i = 0;
88 for (Point p : strip.points) {
89 float fV = max(-1, 1 - dist(p.fx/2., p.fy, fX.getValuef()/2., fY.getValuef()) / 64.);
90 colors[p.index] = color(
91 (lx.getBaseHuef() + 0.3 * abs(p.fx - hOffX.getValuef())) % 360,
92 constrain(80 + 40 * fV, 0, 100),
93 constrain(100 - (30 - fV * falloff.getValuef()) * modDist(i + (s*63)%61, offset.getValuef(), 16), 0, 100)
94 );
95 ++i;
96 }
97 ++s;
98 }
99 }
100 }
101
102 class SwipeTransition extends SCTransition {
103
104 final BasicParameter bleed = new BasicParameter("WIDTH", 0.5);
105
106 SwipeTransition(GLucose glucose) {
107 super(glucose);
108 setDuration(5000);
109 addParameter(bleed);
110 }
111
112 void computeBlend(int[] c1, int[] c2, double progress) {
113 float bleedf = 10 + bleed.getValuef() * 200.;
114 float xPos = (float) (-bleedf + progress * (model.xMax + bleedf));
115 for (Point p : model.points) {
116 float d = (p.fx - xPos) / bleedf;
117 if (d < 0) {
118 colors[p.index] = c2[p.index];
119 } else if (d > 1) {
120 colors[p.index] = c1[p.index];
121 } else {
122 colors[p.index] = lerpColor(c2[p.index], c1[p.index], d, RGB);
123 }
124 }
125 }
126 }
127
128 class CubeEQ extends SCPattern {
129
130 private FFT fft = null;
131 private LinearEnvelope[] bandVals = null;
132 private int avgSize;
133
134 private final BasicParameter thrsh = new BasicParameter("LVL", 0.35);
135 private final BasicParameter range = new BasicParameter("RANG", 0.45);
136 private final BasicParameter edge = new BasicParameter("EDGE", 0.5);
137 private final BasicParameter speed = new BasicParameter("SPD", 0.5);
138 private final BasicParameter tone = new BasicParameter("TONE", 0.5);
139 private final BasicParameter clr = new BasicParameter("CLR", 0.5);
140
141 public CubeEQ(GLucose glucose) {
142 super(glucose);
143 addParameter(thrsh);
144 addParameter(range);
145 addParameter(edge);
146 addParameter(speed);
147 addParameter(tone);
148 addParameter(clr);
149 }
150
151 protected void onActive() {
152 if (this.fft == null) {
153 this.fft = new FFT(lx.audioInput().bufferSize(), lx.audioInput().sampleRate());
154 this.fft.window(FFT.HAMMING);
155 this.fft.logAverages(40, 1);
156 this.avgSize = this.fft.avgSize();
157 this.bandVals = new LinearEnvelope[this.avgSize];
158 for (int i = 0; i < this.bandVals.length; ++i) {
159 this.addModulator(this.bandVals[i] = (new LinearEnvelope(0, 0, 700+i*4))).trigger();
160 }
161 }
162 }
163
164 public void run(int deltaMs) {
165 this.fft.forward(this.lx.audioInput().mix);
166 float toneConst = .35 + .4 * (tone.getValuef() - 0.5);
167 float edgeConst = 2 + 30*(edge.getValuef()*edge.getValuef()*edge.getValuef());
168
169 for (int i = 0; i < avgSize; ++i) {
170 float value = this.fft.getAvg(i);
171 value = 20*log(1 + sqrt(value));
172 float sqdist = avgSize - i;
173 value -= toneConst*sqdist*sqdist + .5*sqdist;
174 value *= 6;
175 if (value > this.bandVals[i].getValue()) {
176 this.bandVals[i].setEndVal(value, 40).trigger();
177 }
178 else {
179 this.bandVals[i].setEndVal(value, 1000 - 900*speed.getValuef()).trigger();
180 }
181 }
182
183 float jBase = 120 - 360*thrsh.getValuef();
184 float jConst = 300.*(1-range.getValuef());
185 float clrConst = 1.1 + clr.getValuef();
186
187 for (Point p : model.points) {
188 float avgIndex = constrain((p.fx / model.xMax * avgSize), 0, avgSize-2);
189 int avgFloor = (int) avgIndex;
190 float j = jBase + jConst * (p.fy / model.yMax);
191 float value = lerp(
192 this.bandVals[avgFloor].getValuef(),
193 this.bandVals[avgFloor+1].getValuef(),
194 avgIndex-avgFloor
195 );
196
197 float b = constrain(edgeConst * (value - j), 0, 100);
198 colors[p.index] = color(
199 (480 + lx.getBaseHuef() - min(clrConst*p.fy, 120)) % 360,
200 100,
201 b);
202 }
203 }
204 }
205
206 class BoomEffect extends SCEffect {
207
208 final BasicParameter falloff = new BasicParameter("WIDTH", 0.5);
209 final BasicParameter speed = new BasicParameter("SPD", 0.5);
210 final BasicParameter bright = new BasicParameter("BRT", 1.0);
211 final BasicParameter sat = new BasicParameter("SAT", 0.2);
212 List<Layer> layers = new ArrayList<Layer>();
213 final float maxr = sqrt(model.xMax*model.xMax + model.yMax*model.yMax + model.zMax*model.zMax) + 10;
214
215 class Layer {
216 LinearEnvelope boom = new LinearEnvelope(-40, 500, 1300);
217
218 Layer() {
219 addModulator(boom);
220 trigger();
221 }
222
223 void trigger() {
224 float falloffv = falloffv();
225 boom.setRange(-100 / falloffv, maxr + 100/falloffv, 4000 - speed.getValuef() * 3300);
226 boom.trigger();
227 }
228
229 void doApply(int[] colors) {
230 float brightv = 100 * bright.getValuef();
231 float falloffv = falloffv();
232 float satv = sat.getValuef() * 100;
233 float huev = lx.getBaseHuef();
234 for (Point p : model.points) {
235 colors[p.index] = blendColor(
236 colors[p.index],
237 color(huev, satv, constrain(brightv - falloffv*abs(boom.getValuef() - dist(p.fx, 2*p.fy, 3*p.fz, model.xMax/2, model.yMax, model.zMax*1.5)), 0, 100)),
238 ADD);
239 }
240 }
241 }
242
243 BoomEffect(GLucose glucose) {
244 super(glucose, true);
245 addParameter(falloff);
246 addParameter(speed);
247 addParameter(bright);
248 addParameter(sat);
249 }
250
251 public void onEnable() {
252 for (Layer l : layers) {
253 if (!l.boom.isRunning()) {
254 l.trigger();
255 return;
256 }
257 }
258 layers.add(new Layer());
259 }
260
261 private float falloffv() {
262 return 20 - 19 * falloff.getValuef();
263 }
264
265 public void onTrigger() {
266 onEnable();
267 }
268
269 public void doApply(int[] colors) {
270 for (Layer l : layers) {
271 if (l.boom.isRunning()) {
272 l.doApply(colors);
273 }
274 }
275 }
276 }
277
278 public class PianoKeyPattern extends SCPattern {
279
280 final LinearEnvelope[] cubeBrt;
281 final SinLFO base[];
282 final BasicParameter attack = new BasicParameter("ATK", 0.1);
283 final BasicParameter release = new BasicParameter("REL", 0.5);
284 final BasicParameter level = new BasicParameter("AMB", 0.6);
285
286 PianoKeyPattern(GLucose glucose) {
287 super(glucose);
288
289 for (MidiInputDevice input : RWMidi.getInputDevices()) {
290 input.createInput(this);
291 }
292
293 addParameter(attack);
294 addParameter(release);
295 addParameter(level);
296 cubeBrt = new LinearEnvelope[model.cubes.size() / 4];
297 for (int i = 0; i < cubeBrt.length; ++i) {
298 addModulator(cubeBrt[i] = new LinearEnvelope(0, 0, 100));
299 }
300 base = new SinLFO[model.cubes.size() / 12];
301 for (int i = 0; i < base.length; ++i) {
302 addModulator(base[i] = new SinLFO(0, 1, 7000 + 1000*i)).trigger();
303 }
304 }
305
306 private float getAttackTime() {
307 return 15 + attack.getValuef()*attack.getValuef() * 2000;
308 }
309
310 private float getReleaseTime() {
311 return 15 + release.getValuef() * 3000;
312 }
313
314 private LinearEnvelope getEnvelope(int index) {
315 return cubeBrt[index % cubeBrt.length];
316 }
317
318 private SinLFO getBase(int index) {
319 return base[index % base.length];
320 }
321
322 public void noteOnReceived(Note note) {
323 LinearEnvelope env = getEnvelope(note.getPitch());
324 env.setEndVal(min(1, env.getValuef() + (note.getVelocity() / 127.)), getAttackTime()).start();
325 }
326
327 public void noteOffReceived(Note note) {
328 getEnvelope(note.getPitch()).setEndVal(0, getReleaseTime()).start();
329 }
330
331 public void run(int deltaMs) {
332 int i = 0;
333 float huef = lx.getBaseHuef();
334 float levelf = level.getValuef();
335 for (Cube c : model.cubes) {
336 float v = max(getBase(i).getValuef() * levelf/4., getEnvelope(i++).getValuef());
337 setColor(c, color(
338 (huef + 20*v + abs(c.cx-model.xMax/2.)*.3 + c.cy) % 360,
339 min(100, 120*v),
340 100*v
341 ));
342 }
343 }
344 }
345
346 class CrossSections extends SCPattern {
347
348 final SinLFO x = new SinLFO(0, model.xMax, 5000);
349 final SinLFO y = new SinLFO(0, model.yMax, 6000);
350 final SinLFO z = new SinLFO(0, model.zMax, 7000);
351
352 final BasicParameter xw = new BasicParameter("XWID", 0.3);
353 final BasicParameter yw = new BasicParameter("YWID", 0.3);
354 final BasicParameter zw = new BasicParameter("ZWID", 0.3);
355 final BasicParameter xr = new BasicParameter("XRAT", 0.7);
356 final BasicParameter yr = new BasicParameter("YRAT", 0.6);
357 final BasicParameter zr = new BasicParameter("ZRAT", 0.5);
358 final BasicParameter xl = new BasicParameter("XLEV", 1);
359 final BasicParameter yl = new BasicParameter("YLEV", 1);
360 final BasicParameter zl = new BasicParameter("ZLEV", 0.5);
361
362
363 CrossSections(GLucose glucose) {
364 super(glucose);
365 addModulator(x).trigger();
366 addModulator(y).trigger();
367 addModulator(z).trigger();
368 addParameter(xr);
369 addParameter(yr);
370 addParameter(zr);
371 addParameter(xw);
372 addParameter(xl);
373 addParameter(yl);
374 addParameter(zl);
375 addParameter(yw);
376 addParameter(zw);
377 }
378
379 void onParameterChanged(LXParameter p) {
380 if (p == xr) {
381 x.setDuration(10000 - 8800*p.getValuef());
382 } else if (p == yr) {
383 y.setDuration(10000 - 9000*p.getValuef());
384 } else if (p == zr) {
385 z.setDuration(10000 - 9000*p.getValuef());
386 }
387 }
388
389 public void run(int deltaMs) {
390 float xv = x.getValuef();
391 float yv = y.getValuef();
392 float zv = z.getValuef();
393 float xlv = 100*xl.getValuef();
394 float ylv = 100*yl.getValuef();
395 float zlv = 100*zl.getValuef();
396
397 float xwv = 100. / (10 + 40*xw.getValuef());
398 float ywv = 100. / (10 + 40*yw.getValuef());
399 float zwv = 100. / (10 + 40*zw.getValuef());
400
401 for (Point p : model.points) {
402 color c = 0;
403 c = blendColor(c, color(
404 (lx.getBaseHuef() + p.fx/10 + p.fy/3) % 360,
405 constrain(140 - 1.1*abs(p.fx - model.xMax/2.), 0, 100),
406 max(0, xlv - xwv*abs(p.fx - xv))
407 ), ADD);
408 c = blendColor(c, color(
409 (lx.getBaseHuef() + 80 + p.fy/10) % 360,
410 constrain(140 - 2.2*abs(p.fy - model.yMax/2.), 0, 100),
411 max(0, ylv - ywv*abs(p.fy - yv))
412 ), ADD);
413 c = blendColor(c, color(
414 (lx.getBaseHuef() + 160 + p.fz / 10 + p.fy/2) % 360,
415 constrain(140 - 2.2*abs(p.fz - model.zMax/2.), 0, 100),
416 max(0, zlv - zwv*abs(p.fz - zv))
417 ), ADD);
418 colors[p.index] = c;
419 }
420 }
421 }
422
423 class Blinders extends SCPattern {
424
425 final SinLFO[] m;
426 final TriangleLFO r;
427 final SinLFO s;
428 final TriangleLFO hs;
429
430 public Blinders(GLucose glucose) {
431 super(glucose);
432 m = new SinLFO[12];
433 for (int i = 0; i < m.length; ++i) {
434 addModulator(m[i] = new SinLFO(0.5, 120, (120000. / (3+i)))).trigger();
435 }
436 addModulator(r = new TriangleLFO(9000, 15000, 29000)).trigger();
437 addModulator(s = new SinLFO(-20, 275, 11000)).trigger();
438 addModulator(hs = new TriangleLFO(0.1, 0.5, 15000)).trigger();
439 s.modulateDurationBy(r);
440 }
441
442 public void run(int deltaMs) {
443 float hv = lx.getBaseHuef();
444 int si = 0;
445 for (Strip strip : model.strips) {
446 int i = 0;
447 float mv = m[si % m.length].getValuef();
448 for (Point p : strip.points) {
449 colors[p.index] = color(
450 (hv + p.fz + p.fy*hs.getValuef()) % 360,
451 min(100, abs(p.fx - s.getValuef())/2.),
452 max(0, 100 - mv/2. - mv * abs(i - 7.5))
453 );
454 ++i;
455 }
456 ++si;
457 }
458 }
459 }
460
461 class Psychedelia extends SCPattern {
462
463 final int NUM = 3;
464 SinLFO m = new SinLFO(-0.5, NUM-0.5, 9000);
465 SinLFO s = new SinLFO(-20, 147, 11000);
466 TriangleLFO h = new TriangleLFO(0, 240, 19000);
467 SinLFO c = new SinLFO(-.2, .8, 31000);
468
469 Psychedelia(GLucose glucose) {
470 super(glucose);
471 addModulator(m).trigger();
472 addModulator(s).trigger();
473 addModulator(h).trigger();
474 addModulator(c).trigger();
475 }
476
477 void run(int deltaMs) {
478 float huev = h.getValuef();
479 float cv = c.getValuef();
480 float sv = s.getValuef();
481 float mv = m.getValuef();
482 int i = 0;
483 for (Strip strip : model.strips) {
484 for (Point p : strip.points) {
485 colors[p.index] = color(
486 (huev + i*constrain(cv, 0, 2) + p.fz/2. + p.fx/4.) % 360,
487 min(100, abs(p.fy-sv)),
488 max(0, 100 - 50*abs((i%NUM) - mv))
489 );
490 }
491 ++i;
492 }
493 }
494 }
495
496 class AskewPlanes extends SCPattern {
497
498 class Plane {
499 private final SinLFO a;
500 private final SinLFO b;
501 private final SinLFO c;
502 float av;
503 float bv;
504 float cv;
505 float denom;
506
507 Plane(int i) {
508 addModulator(a = new SinLFO(-1, 1, 4000 + 1029*i)).trigger();
509 addModulator(b = new SinLFO(-1, 1, 11000 - 1104*i)).trigger();
510 addModulator(c = new SinLFO(-50, 50, 4000 + 1000*i * ((i % 2 == 0) ? 1 : -1))).trigger();
511 }
512
513 void run(int deltaMs) {
514 av = a.getValuef();
515 bv = b.getValuef();
516 cv = c.getValuef();
517 denom = sqrt(av*av + bv*bv);
518 }
519 }
520
521 final Plane[] planes;
522 final int NUM_PLANES = 3;
523
524 AskewPlanes(GLucose glucose) {
525 super(glucose);
526 planes = new Plane[NUM_PLANES];
527 for (int i = 0; i < planes.length; ++i) {
528 planes[i] = new Plane(i);
529 }
530 }
531
532 private final float denoms[] = new float[NUM_PLANES];
533
534 public void run(int deltaMs) {
535 float huev = lx.getBaseHuef();
536 int i = 0;
537 for (Plane p : planes) {
538 p.run(deltaMs);
539 }
540 for (Point p : model.points) {
541 float d = MAX_FLOAT;
542 for (Plane plane : planes) {
543 d = min(d, abs(plane.av*(p.fx-model.xMax/2.) + plane.bv*(p.fy-model.yMax/2.) + plane.cv) / plane.denom);
544 }
545 colors[p.index] = color(
546 (lx.getBaseHuef() + abs(p.fx-model.xMax/2.)*.3 + p.fy*.8) % 360,
547 max(0, 100 - .8*abs(p.fx - model.xMax/2.)),
548 constrain(140 - 10.*d, 0, 100)
549 );
550 }
551 }
552 }
553
554 class ShiftingPlane extends SCPattern {
555
556 final SinLFO a = new SinLFO(-.2, .2, 5300);
557 final SinLFO b = new SinLFO(1, -1, 13300);
558 final SinLFO c = new SinLFO(-1.4, 1.4, 5700);
559 final SinLFO d = new SinLFO(-10, 10, 9500);
560
561 ShiftingPlane(GLucose glucose) {
562 super(glucose);
563 addModulator(a).trigger();
564 addModulator(b).trigger();
565 addModulator(c).trigger();
566 addModulator(d).trigger();
567 }
568
569 public void run(int deltaMs) {
570 float hv = lx.getBaseHuef();
571 float av = a.getValuef();
572 float bv = b.getValuef();
573 float cv = c.getValuef();
574 float dv = d.getValuef();
575 float denom = sqrt(av*av + bv*bv + cv*cv);
576 for (Point p : model.points) {
577 float d = abs(av*(p.fx-model.xMax/2.) + bv*(p.fy-model.yMax/2.) + cv*(p.fz-model.zMax/2.) + dv) / denom;
578 colors[p.index] = color(
579 (hv + abs(p.fx-model.xMax/2.)*.6 + abs(p.fy-model.yMax/2)*.9 + abs(p.fz - model.zMax/2.)) % 360,
580 constrain(110 - d*6, 0, 100),
581 constrain(130 - 7*d, 0, 100)
582 );
583 }
584 }
585 }
586