MidiMusic pattern in progress and some framework changes
[SugarCubes.git] / MarkSlee.pde
1 class MidiMusic extends SCPattern {
2
3 private final Map<Integer, LightUp> lightMap = new HashMap<Integer, LightUp>();
4 private final List<LightUp> allLights = new ArrayList<LightUp>();
5
6 private final Stack<LightUp> newLayers = new Stack<LightUp>();
7
8 MidiMusic(GLucose glucose) {
9 super(glucose);
10 }
11
12 class LightUp extends LXLayer {
13
14 private LinearEnvelope brt = new LinearEnvelope(0, 0, 0);
15 private Accelerator yPos = new Accelerator(0, 0, 0);
16 private float xPos;
17
18 LightUp() {
19 addModulator(brt);
20 addModulator(yPos);
21 }
22
23 boolean isAvailable() {
24 return brt.getValuef() <= 0;
25 }
26
27 void noteOn(Note note) {
28 xPos = lerp(0, model.xMax, constrain(0.5 + (note.getPitch() - 64) / 12., 0, 1));
29 yPos.setValue(lerp(20, model.yMax, note.getVelocity() / 127.));
30 brt.setRangeFromHereTo(lerp(40, 100, note.getVelocity() / 127.), 20).start();
31 }
32
33 void noteOff(Note note) {
34 yPos.setVelocity(0).setAcceleration(-380).start();
35 brt.setRangeFromHereTo(0, 1000).start();
36 }
37
38 public void run(double deltaMs, color[] colors) {
39 float bVal = brt.getValuef();
40 if (bVal <= 0) {
41 return;
42 }
43 float yVal = yPos.getValuef();
44 for (Point p : model.points) {
45 float b = max(0, bVal - 3*dist(p.x, p.y, xPos, yVal));
46 if (b > 0) {
47 colors[p.index] = blendColor(colors[p.index], color(
48 (lx.getBaseHuef() + abs(p.x - model.cx) + abs(p.y - model.cy)) % 360,
49 100,
50 b
51 ), ADD);
52 }
53 }
54 }
55 }
56
57 public synchronized boolean noteOnReceived(Note note) {
58 if (note.getChannel() == 0) {
59 for (LightUp light : allLights) {
60 if (light.isAvailable()) {
61 light.noteOn(note);
62 lightMap.put(note.getPitch(), light);
63 return true;
64 }
65 }
66 LightUp newLight = new LightUp();
67 newLight.noteOn(note);
68 lightMap.put(note.getPitch(), newLight);
69 synchronized(newLayers) {
70 newLayers.push(newLight);
71 }
72 } else if (note.getChannel() == 1) {
73 }
74 return true;
75 }
76
77 public synchronized boolean noteOffReceived(Note note) {
78 if (note.getChannel() == 0) {
79 LightUp light = lightMap.get(note.getPitch());
80 if (light != null) {
81 light.noteOff(note);
82 }
83 }
84 return true;
85 }
86
87 public synchronized void run(double deltaMs) {
88 setColors(#000000);
89 if (!newLayers.isEmpty()) {
90 synchronized(newLayers) {
91 while (!newLayers.isEmpty()) {
92 addLayer(newLayers.pop());
93 }
94 }
95 }
96 }
97 }
98
99 class Pulley extends SCPattern {
100
101 final int NUM_DIVISIONS = 16;
102 private final Accelerator[] gravity = new Accelerator[NUM_DIVISIONS];
103 private final Click[] delays = new Click[NUM_DIVISIONS];
104
105 private final Click reset = new Click(9000);
106 private boolean isRising = false;
107
108 private BasicParameter sz = new BasicParameter("SIZE", 0.5);
109 private BasicParameter beatAmount = new BasicParameter("BEAT", 0.1);
110
111 Pulley(GLucose glucose) {
112 super(glucose);
113 for (int i = 0; i < NUM_DIVISIONS; ++i) {
114 addModulator(gravity[i] = new Accelerator(0, 0, 0));
115 addModulator(delays[i] = new Click(0));
116 }
117 addModulator(reset).start();
118 addParameter(sz);
119 addParameter(beatAmount);
120 trigger();
121 }
122
123 private void trigger() {
124 isRising = !isRising;
125 int i = 0;
126 for (Accelerator g : gravity) {
127 if (isRising) {
128 g.setSpeed(random(40, 50), 0).start();
129 } else {
130 g.setVelocity(0).setAcceleration(-420);
131 delays[i].setDuration(random(0, 500)).trigger();
132 }
133 ++i;
134 }
135 }
136
137 public void run(double deltaMs) {
138 if (reset.click()) {
139 trigger();
140 }
141 int j = 0;
142 for (Click d : delays) {
143 if (d.click()) {
144 gravity[j].start();
145 d.stop();
146 }
147 ++j;
148 }
149
150 for (Accelerator g : gravity) {
151 if (isRising) {
152 if (g.getValuef() > model.yMax) {
153 g.stop();
154 } else if (g.getValuef() > model.yMax*.55) {
155 if (g.getVelocityf() > 10) {
156 g.setAcceleration(-16);
157 } else {
158 g.setAcceleration(0);
159 }
160 }
161 } else {
162 if (g.getValuef() < 0) {
163 g.setValue(-g.getValuef());
164 g.setVelocity(-g.getVelocityf() * random(0.74, 0.84));
165 }
166 }
167 }
168
169 float fPos = 1 - lx.tempo.rampf();
170 if (fPos < .2) {
171 fPos = .2 + 4 * (.2 - fPos);
172 }
173 float falloff = 100. / (3 + sz.getValuef() * 36 + fPos * beatAmount.getValuef()*48);
174 for (Point p : model.points) {
175 int i = (int) constrain((p.x - model.xMin) * NUM_DIVISIONS / (model.xMax - model.xMin), 0, NUM_DIVISIONS-1);
176 colors[p.index] = color(
177 (lx.getBaseHuef() + abs(p.x - model.cx)*.8 + p.y*.4) % 360,
178 constrain(130 - p.y*.8, 0, 100),
179 max(0, 100 - abs(p.y - gravity[i].getValuef())*falloff)
180 );
181 }
182 }
183 }
184
185 class ViolinWave extends SCPattern {
186
187 BasicParameter level = new BasicParameter("LVL", 0.45);
188 BasicParameter range = new BasicParameter("RNG", 0.5);
189 BasicParameter edge = new BasicParameter("EDG", 0.5);
190 BasicParameter release = new BasicParameter("RLS", 0.5);
191 BasicParameter speed = new BasicParameter("SPD", 0.5);
192 BasicParameter amp = new BasicParameter("AMP", 0.25);
193 BasicParameter period = new BasicParameter("WAVE", 0.5);
194 BasicParameter pSize = new BasicParameter("PSIZE", 0.5);
195 BasicParameter pSpeed = new BasicParameter("PSPD", 0.5);
196 BasicParameter pDensity = new BasicParameter("PDENS", 0.25);
197
198 LinearEnvelope dbValue = new LinearEnvelope(0, 0, 10);
199
200 ViolinWave(GLucose glucose) {
201 super(glucose);
202 addParameter(level);
203 addParameter(edge);
204 addParameter(range);
205 addParameter(release);
206 addParameter(speed);
207 addParameter(amp);
208 addParameter(period);
209 addParameter(pSize);
210 addParameter(pSpeed);
211 addParameter(pDensity);
212
213 addModulator(dbValue);
214 }
215
216 final List<Particle> particles = new ArrayList<Particle>();
217
218 class Particle {
219
220 LinearEnvelope x = new LinearEnvelope(0, 0, 0);
221 LinearEnvelope y = new LinearEnvelope(0, 0, 0);
222
223 Particle() {
224 addModulator(x);
225 addModulator(y);
226 }
227
228 Particle trigger(boolean direction) {
229 float xInit = random(model.xMin, model.xMax);
230 float time = 3000 - 2500*pSpeed.getValuef();
231 x.setRange(xInit, xInit + random(-40, 40), time).trigger();
232 y.setRange(model.cy + 10, direction ? model.yMax + 50 : model.yMin - 50, time).trigger();
233 return this;
234 }
235
236 boolean isActive() {
237 return x.isRunning() || y.isRunning();
238 }
239
240 public void run(double deltaMs) {
241 if (!isActive()) {
242 return;
243 }
244
245 float pFalloff = (30 - 27*pSize.getValuef());
246 for (Point p : model.points) {
247 float b = 100 - pFalloff * (abs(p.x - x.getValuef()) + abs(p.y - y.getValuef()));
248 if (b > 0) {
249 colors[p.index] = blendColor(colors[p.index], color(
250 lx.getBaseHuef(), 20, b
251 ), ADD);
252 }
253 }
254 }
255 }
256
257 float[] centers = new float[30];
258 double accum = 0;
259 boolean rising = true;
260
261 void fireParticle(boolean direction) {
262 boolean gotOne = false;
263 for (Particle p : particles) {
264 if (!p.isActive()) {
265 p.trigger(direction);
266 return;
267 }
268 }
269 particles.add(new Particle().trigger(direction));
270 }
271
272 public void run(double deltaMs) {
273 accum += deltaMs / (1000. - 900.*speed.getValuef());
274 for (int i = 0; i < centers.length; ++i) {
275 centers[i] = model.cy + 30*amp.getValuef()*sin((float) (accum + (i-centers.length/2.)/(1. + 9.*period.getValuef())));
276 }
277
278 float zeroDBReference = pow(10, (50 - 190*level.getValuef())/20.);
279 float dB = 20*GraphicEQ.log10(lx.audioInput().mix.level() / zeroDBReference);
280 if (dB > dbValue.getValuef()) {
281 rising = true;
282 dbValue.setRangeFromHereTo(dB, 10).trigger();
283 } else {
284 if (rising) {
285 for (int j = 0; j < pDensity.getValuef()*3; ++j) {
286 fireParticle(true);
287 fireParticle(false);
288 }
289 }
290 rising = false;
291 dbValue.setRangeFromHereTo(max(dB, -96), 50 + 1000*release.getValuef()).trigger();
292 }
293 float edg = 1 + edge.getValuef() * 40;
294 float rng = (78 - 64 * range.getValuef()) / (model.yMax - model.cy);
295 float val = max(2, dbValue.getValuef());
296
297 for (Point p : model.points) {
298 int ci = (int) lerp(0, centers.length-1, (p.x - model.xMin) / (model.xMax - model.xMin));
299 float rFactor = 1.0 - 0.9 * abs(p.x - model.cx) / (model.xMax - model.cx);
300 colors[p.index] = color(
301 (lx.getBaseHuef() + abs(p.x - model.cx)) % 360,
302 min(100, 20 + 8*abs(p.y - centers[ci])),
303 constrain(edg*(val*rFactor - rng * abs(p.y-centers[ci])), 0, 100)
304 );
305 }
306
307 for (Particle p : particles) {
308 p.run(deltaMs);
309 }
310 }
311 }
312
313 class BouncyBalls extends SCPattern {
314
315 static final int NUM_BALLS = 6;
316
317 class BouncyBall {
318
319 Accelerator yPos;
320 TriangleLFO xPos = new TriangleLFO(0, model.xMax, random(8000, 19000));
321 float zPos;
322
323 BouncyBall(int i) {
324 addModulator(xPos).setBasis(random(0, TWO_PI)).start();
325 addModulator(yPos = new Accelerator(0, 0, 0));
326 zPos = lerp(model.zMin, model.zMax, (i+2.) / (NUM_BALLS + 4.));
327 }
328
329 void bounce(float midiVel) {
330 float v = 100 + 8*midiVel;
331 yPos.setSpeed(v, getAccel(v, 60 / lx.tempo.bpmf())).start();
332 }
333
334 float getAccel(float v, float oneBeat) {
335 return -2*v / oneBeat;
336 }
337
338 void run(double deltaMs) {
339 float flrLevel = flr.getValuef() * model.xMax/2.;
340 if (yPos.getValuef() < flrLevel) {
341 if (yPos.getVelocity() < -50) {
342 yPos.setValue(2*flrLevel-yPos.getValuef());
343 float v = -yPos.getVelocityf() * bounce.getValuef();
344 yPos.setSpeed(v, getAccel(v, 60 / lx.tempo.bpmf()));
345 } else {
346 yPos.setValue(flrLevel).stop();
347 }
348 }
349 float falloff = 130.f / (12 + blobSize.getValuef() * 36);
350 float xv = xPos.getValuef();
351 float yv = yPos.getValuef();
352
353 for (Point p : model.points) {
354 float d = sqrt((p.x-xv)*(p.x-xv) + (p.y-yv)*(p.y-yv) + .1*(p.z-zPos)*(p.z-zPos));
355 float b = constrain(130 - falloff*d, 0, 100);
356 if (b > 0) {
357 colors[p.index] = blendColor(colors[p.index], color(
358 (lx.getBaseHuef() + p.y*.5 + abs(model.cx - p.x) * .5) % 360,
359 max(0, 100 - .45*(p.y - flrLevel)),
360 b
361 ), ADD);
362 }
363 }
364 }
365 }
366
367 final BouncyBall[] balls = new BouncyBall[NUM_BALLS];
368
369 final BasicParameter bounce = new BasicParameter("BNC", .8);
370 final BasicParameter flr = new BasicParameter("FLR", 0);
371 final BasicParameter blobSize = new BasicParameter("SIZE", 0.5);
372
373 BouncyBalls(GLucose glucose) {
374 super(glucose);
375 for (int i = 0; i < balls.length; ++i) {
376 balls[i] = new BouncyBall(i);
377 }
378 addParameter(bounce);
379 addParameter(flr);
380 addParameter(blobSize);
381 }
382
383 public void run(double deltaMs) {
384 setColors(#000000);
385 for (BouncyBall b : balls) {
386 b.run(deltaMs);
387 }
388 }
389
390 public boolean noteOnReceived(Note note) {
391 int pitch = (note.getPitch() + note.getChannel()) % NUM_BALLS;
392 balls[pitch].bounce(note.getVelocity());
393 return true;
394 }
395 }
396
397 class SpaceTime extends SCPattern {
398
399 SinLFO pos = new SinLFO(0, 1, 3000);
400 SinLFO rate = new SinLFO(1000, 9000, 13000);
401 SinLFO falloff = new SinLFO(10, 70, 5000);
402 float angle = 0;
403
404 BasicParameter rateParameter = new BasicParameter("RATE", 0.5);
405 BasicParameter sizeParameter = new BasicParameter("SIZE", 0.5);
406
407
408 public SpaceTime(GLucose glucose) {
409 super(glucose);
410
411 addModulator(pos).trigger();
412 addModulator(rate).trigger();
413 addModulator(falloff).trigger();
414 pos.modulateDurationBy(rate);
415 addParameter(rateParameter);
416 addParameter(sizeParameter);
417 }
418
419 public void onParameterChanged(LXParameter parameter) {
420 if (parameter == rateParameter) {
421 rate.stop().setValue(9000 - 8000*parameter.getValuef());
422 } else if (parameter == sizeParameter) {
423 falloff.stop().setValue(70 - 60*parameter.getValuef());
424 }
425 }
426
427 void run(double deltaMs) {
428 angle += deltaMs * 0.0007;
429 float sVal1 = model.strips.size() * (0.5 + 0.5*sin(angle));
430 float sVal2 = model.strips.size() * (0.5 + 0.5*cos(angle));
431
432 float pVal = pos.getValuef();
433 float fVal = falloff.getValuef();
434
435 int s = 0;
436 for (Strip strip : model.strips) {
437 int i = 0;
438 for (Point p : strip.points) {
439 colors[p.index] = color(
440 (lx.getBaseHuef() + 360 - p.fx*.2 + p.fy * .3) % 360,
441 constrain(.4 * min(abs(s - sVal1), abs(s - sVal2)), 20, 100),
442 max(0, 100 - fVal*abs(i - pVal*(strip.metrics.numPoints - 1)))
443 );
444 ++i;
445 }
446 ++s;
447 }
448 }
449 }
450
451 class Swarm extends SCPattern {
452
453 SawLFO offset = new SawLFO(0, 1, 1000);
454 SinLFO rate = new SinLFO(350, 1200, 63000);
455 SinLFO falloff = new SinLFO(15, 50, 17000);
456 SinLFO fX = new SinLFO(0, model.xMax, 19000);
457 SinLFO fY = new SinLFO(0, model.yMax, 11000);
458 SinLFO hOffX = new SinLFO(0, model.xMax, 13000);
459
460 public Swarm(GLucose glucose) {
461 super(glucose);
462
463 addModulator(offset).trigger();
464 addModulator(rate).trigger();
465 addModulator(falloff).trigger();
466 addModulator(fX).trigger();
467 addModulator(fY).trigger();
468 addModulator(hOffX).trigger();
469 offset.modulateDurationBy(rate);
470 }
471
472 float modDist(float v1, float v2, float mod) {
473 v1 = v1 % mod;
474 v2 = v2 % mod;
475 if (v2 > v1) {
476 return min(v2-v1, v1+mod-v2);
477 }
478 else {
479 return min(v1-v2, v2+mod-v1);
480 }
481 }
482
483 void run(double deltaMs) {
484 float s = 0;
485 for (Strip strip : model.strips ) {
486 int i = 0;
487 for (Point p : strip.points) {
488 float fV = max(-1, 1 - dist(p.fx/2., p.fy, fX.getValuef()/2., fY.getValuef()) / 64.);
489 colors[p.index] = color(
490 (lx.getBaseHuef() + 0.3 * abs(p.fx - hOffX.getValuef())) % 360,
491 constrain(80 + 40 * fV, 0, 100),
492 constrain(100 - (30 - fV * falloff.getValuef()) * modDist(i + (s*63)%61, offset.getValuef() * strip.metrics.numPoints, strip.metrics.numPoints), 0, 100)
493 );
494 ++i;
495 }
496 ++s;
497 }
498 }
499 }
500
501 class SwipeTransition extends SCTransition {
502
503 final BasicParameter bleed = new BasicParameter("WIDTH", 0.5);
504
505 SwipeTransition(GLucose glucose) {
506 super(glucose);
507 setDuration(5000);
508 addParameter(bleed);
509 }
510
511 void computeBlend(int[] c1, int[] c2, double progress) {
512 float bleedf = 10 + bleed.getValuef() * 200.;
513 float xPos = (float) (-bleedf + progress * (model.xMax + bleedf));
514 for (Point p : model.points) {
515 float d = (p.fx - xPos) / bleedf;
516 if (d < 0) {
517 colors[p.index] = c2[p.index];
518 } else if (d > 1) {
519 colors[p.index] = c1[p.index];
520 } else {
521 colors[p.index] = lerpColor(c2[p.index], c1[p.index], d, RGB);
522 }
523 }
524 }
525 }
526
527 abstract class BlendTransition extends SCTransition {
528
529 final int blendType;
530
531 BlendTransition(GLucose glucose, int blendType) {
532 super(glucose);
533 this.blendType = blendType;
534 }
535
536 void computeBlend(int[] c1, int[] c2, double progress) {
537 if (progress < 0.5) {
538 for (int i = 0; i < c1.length; ++i) {
539 colors[i] = lerpColor(
540 c1[i],
541 blendColor(c1[i], c2[i], blendType),
542 (float) (2.*progress),
543 RGB);
544 }
545 } else {
546 for (int i = 0; i < c1.length; ++i) {
547 colors[i] = lerpColor(
548 c2[i],
549 blendColor(c1[i], c2[i], blendType),
550 (float) (2.*(1. - progress)),
551 RGB);
552 }
553 }
554 }
555 }
556
557 class MultiplyTransition extends BlendTransition {
558 MultiplyTransition(GLucose glucose) {
559 super(glucose, MULTIPLY);
560 }
561 }
562
563 class ScreenTransition extends BlendTransition {
564 ScreenTransition(GLucose glucose) {
565 super(glucose, SCREEN);
566 }
567 }
568
569 class BurnTransition extends BlendTransition {
570 BurnTransition(GLucose glucose) {
571 super(glucose, BURN);
572 }
573 }
574
575 class DodgeTransition extends BlendTransition {
576 DodgeTransition(GLucose glucose) {
577 super(glucose, DODGE);
578 }
579 }
580
581 class OverlayTransition extends BlendTransition {
582 OverlayTransition(GLucose glucose) {
583 super(glucose, OVERLAY);
584 }
585 }
586
587 class AddTransition extends BlendTransition {
588 AddTransition(GLucose glucose) {
589 super(glucose, ADD);
590 }
591 }
592
593 class SubtractTransition extends BlendTransition {
594 SubtractTransition(GLucose glucose) {
595 super(glucose, SUBTRACT);
596 }
597 }
598
599 class SoftLightTransition extends BlendTransition {
600 SoftLightTransition(GLucose glucose) {
601 super(glucose, SOFT_LIGHT);
602 }
603 }
604
605 class BassPod extends SCPattern {
606
607 private GraphicEQ eq = null;
608
609 public BassPod(GLucose glucose) {
610 super(glucose);
611 }
612
613 protected void onActive() {
614 if (eq == null) {
615 eq = new GraphicEQ(lx, 16);
616 eq.slope.setValue(0.6);
617 addParameter(eq.level);
618 addParameter(eq.range);
619 addParameter(eq.attack);
620 addParameter(eq.release);
621 addParameter(eq.slope);
622 }
623 }
624
625 public void run(double deltaMs) {
626 eq.run(deltaMs);
627
628 float bassLevel = eq.getAverageLevel(0, 5);
629
630 for (Point p : model.points) {
631 int avgIndex = (int) constrain(1 + abs(p.fx-model.xMax/2.)/(model.xMax/2.)*(eq.numBands-5), 0, eq.numBands-5);
632 float value = 0;
633 for (int i = avgIndex; i < avgIndex + 5; ++i) {
634 value += eq.getLevel(i);
635 }
636 value /= 5.;
637
638 float b = constrain(8 * (value*model.yMax - abs(p.fy-model.yMax/2.)), 0, 100);
639 colors[p.index] = color(
640 (lx.getBaseHuef() + abs(p.fy - model.cy) + abs(p.fx - model.cx)) % 360,
641 constrain(bassLevel*240 - .6*dist(p.fx, p.fy, model.cx, model.cy), 0, 100),
642 b
643 );
644 }
645 }
646 }
647
648
649 class CubeEQ extends SCPattern {
650
651 private GraphicEQ eq = null;
652
653 private final BasicParameter edge = new BasicParameter("EDGE", 0.5);
654 private final BasicParameter clr = new BasicParameter("CLR", 0.5);
655 private final BasicParameter blockiness = new BasicParameter("BLK", 0.5);
656
657 public CubeEQ(GLucose glucose) {
658 super(glucose);
659 }
660
661 protected void onActive() {
662 if (eq == null) {
663 eq = new GraphicEQ(lx, 16);
664 addParameter(eq.level);
665 addParameter(eq.range);
666 addParameter(eq.attack);
667 addParameter(eq.release);
668 addParameter(eq.slope);
669 addParameter(edge);
670 addParameter(clr);
671 addParameter(blockiness);
672 }
673 }
674
675 public void run(double deltaMs) {
676 eq.run(deltaMs);
677
678 float edgeConst = 2 + 30*edge.getValuef();
679 float clrConst = 1.1 + clr.getValuef();
680
681 for (Point p : model.points) {
682 float avgIndex = constrain(2 + p.fx / model.xMax * (eq.numBands-4), 0, eq.numBands-4);
683 int avgFloor = (int) avgIndex;
684
685 float leftVal = eq.getLevel(avgFloor);
686 float rightVal = eq.getLevel(avgFloor+1);
687 float smoothValue = lerp(leftVal, rightVal, avgIndex-avgFloor);
688
689 float chunkyValue = (
690 eq.getLevel(avgFloor/4*4) +
691 eq.getLevel(avgFloor/4*4 + 1) +
692 eq.getLevel(avgFloor/4*4 + 2) +
693 eq.getLevel(avgFloor/4*4 + 3)
694 ) / 4.;
695
696 float value = lerp(smoothValue, chunkyValue, blockiness.getValuef());
697
698 float b = constrain(edgeConst * (value*model.yMax - p.fy), 0, 100);
699 colors[p.index] = color(
700 (480 + lx.getBaseHuef() - min(clrConst*p.fy, 120)) % 360,
701 100,
702 b
703 );
704 }
705 }
706 }
707
708 class BoomEffect extends SCEffect {
709
710 final BasicParameter falloff = new BasicParameter("WIDTH", 0.5);
711 final BasicParameter speed = new BasicParameter("SPD", 0.5);
712 final BasicParameter bright = new BasicParameter("BRT", 1.0);
713 final BasicParameter sat = new BasicParameter("SAT", 0.2);
714 List<Layer> layers = new ArrayList<Layer>();
715 final float maxr = sqrt(model.xMax*model.xMax + model.yMax*model.yMax + model.zMax*model.zMax) + 10;
716
717 class Layer {
718 LinearEnvelope boom = new LinearEnvelope(-40, 500, 1300);
719
720 Layer() {
721 addModulator(boom);
722 trigger();
723 }
724
725 void trigger() {
726 float falloffv = falloffv();
727 boom.setRange(-100 / falloffv, maxr + 100/falloffv, 4000 - speed.getValuef() * 3300);
728 boom.trigger();
729 }
730
731 void doApply(int[] colors) {
732 float brightv = 100 * bright.getValuef();
733 float falloffv = falloffv();
734 float satv = sat.getValuef() * 100;
735 float huev = lx.getBaseHuef();
736 for (Point p : model.points) {
737 colors[p.index] = blendColor(
738 colors[p.index],
739 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)),
740 ADD);
741 }
742 }
743 }
744
745 BoomEffect(GLucose glucose) {
746 super(glucose, true);
747 addParameter(falloff);
748 addParameter(speed);
749 addParameter(bright);
750 addParameter(sat);
751 }
752
753 public void onEnable() {
754 for (Layer l : layers) {
755 if (!l.boom.isRunning()) {
756 l.trigger();
757 return;
758 }
759 }
760 layers.add(new Layer());
761 }
762
763 private float falloffv() {
764 return 20 - 19 * falloff.getValuef();
765 }
766
767 public void onTrigger() {
768 onEnable();
769 }
770
771 public void doApply(int[] colors) {
772 for (Layer l : layers) {
773 if (l.boom.isRunning()) {
774 l.doApply(colors);
775 }
776 }
777 }
778 }
779
780 public class PianoKeyPattern extends SCPattern {
781
782 final LinearEnvelope[] cubeBrt;
783 final SinLFO base[];
784 final BasicParameter attack = new BasicParameter("ATK", 0.1);
785 final BasicParameter release = new BasicParameter("REL", 0.5);
786 final BasicParameter level = new BasicParameter("AMB", 0.6);
787
788 PianoKeyPattern(GLucose glucose) {
789 super(glucose);
790
791 addParameter(attack);
792 addParameter(release);
793 addParameter(level);
794 cubeBrt = new LinearEnvelope[model.cubes.size() / 4];
795 for (int i = 0; i < cubeBrt.length; ++i) {
796 addModulator(cubeBrt[i] = new LinearEnvelope(0, 0, 100));
797 }
798 base = new SinLFO[model.cubes.size() / 12];
799 for (int i = 0; i < base.length; ++i) {
800 addModulator(base[i] = new SinLFO(0, 1, 7000 + 1000*i)).trigger();
801 }
802 }
803
804 private float getAttackTime() {
805 return 15 + attack.getValuef()*attack.getValuef() * 2000;
806 }
807
808 private float getReleaseTime() {
809 return 15 + release.getValuef() * 3000;
810 }
811
812 private LinearEnvelope getEnvelope(int index) {
813 return cubeBrt[index % cubeBrt.length];
814 }
815
816 private SinLFO getBase(int index) {
817 return base[index % base.length];
818 }
819
820 public boolean noteOnReceived(Note note) {
821 LinearEnvelope env = getEnvelope(note.getPitch());
822 env.setEndVal(min(1, env.getValuef() + (note.getVelocity() / 127.)), getAttackTime()).start();
823 return true;
824 }
825
826 public boolean noteOffReceived(Note note) {
827 getEnvelope(note.getPitch()).setEndVal(0, getReleaseTime()).start();
828 return true;
829 }
830
831 public void run(double deltaMs) {
832 int i = 0;
833 float huef = lx.getBaseHuef();
834 float levelf = level.getValuef();
835 for (Cube c : model.cubes) {
836 float v = max(getBase(i).getValuef() * levelf/4., getEnvelope(i++).getValuef());
837 setColor(c, color(
838 (huef + 20*v + abs(c.cx-model.xMax/2.)*.3 + c.cy) % 360,
839 min(100, 120*v),
840 100*v
841 ));
842 }
843 }
844 }
845
846 class CrossSections extends SCPattern {
847
848 final SinLFO x = new SinLFO(0, model.xMax, 5000);
849 final SinLFO y = new SinLFO(0, model.yMax, 6000);
850 final SinLFO z = new SinLFO(0, model.zMax, 7000);
851
852 final BasicParameter xw = new BasicParameter("XWID", 0.3);
853 final BasicParameter yw = new BasicParameter("YWID", 0.3);
854 final BasicParameter zw = new BasicParameter("ZWID", 0.3);
855 final BasicParameter xr = new BasicParameter("XRAT", 0.7);
856 final BasicParameter yr = new BasicParameter("YRAT", 0.6);
857 final BasicParameter zr = new BasicParameter("ZRAT", 0.5);
858 final BasicParameter xl = new BasicParameter("XLEV", 1);
859 final BasicParameter yl = new BasicParameter("YLEV", 1);
860 final BasicParameter zl = new BasicParameter("ZLEV", 0.5);
861
862
863 CrossSections(GLucose glucose) {
864 super(glucose);
865 addModulator(x).trigger();
866 addModulator(y).trigger();
867 addModulator(z).trigger();
868 addParams();
869 }
870
871 protected void addParams() {
872 addParameter(xr);
873 addParameter(yr);
874 addParameter(zr);
875 addParameter(xw);
876 addParameter(xl);
877 addParameter(yl);
878 addParameter(zl);
879 addParameter(yw);
880 addParameter(zw);
881 }
882
883 void onParameterChanged(LXParameter p) {
884 if (p == xr) {
885 x.setDuration(10000 - 8800*p.getValuef());
886 } else if (p == yr) {
887 y.setDuration(10000 - 9000*p.getValuef());
888 } else if (p == zr) {
889 z.setDuration(10000 - 9000*p.getValuef());
890 }
891 }
892
893 float xv, yv, zv;
894
895 protected void updateXYZVals() {
896 xv = x.getValuef();
897 yv = y.getValuef();
898 zv = z.getValuef();
899 }
900
901 public void run(double deltaMs) {
902 updateXYZVals();
903
904 float xlv = 100*xl.getValuef();
905 float ylv = 100*yl.getValuef();
906 float zlv = 100*zl.getValuef();
907
908 float xwv = 100. / (10 + 40*xw.getValuef());
909 float ywv = 100. / (10 + 40*yw.getValuef());
910 float zwv = 100. / (10 + 40*zw.getValuef());
911
912 for (Point p : model.points) {
913 color c = 0;
914 c = blendColor(c, color(
915 (lx.getBaseHuef() + p.fx/10 + p.fy/3) % 360,
916 constrain(140 - 1.1*abs(p.fx - model.xMax/2.), 0, 100),
917 max(0, xlv - xwv*abs(p.fx - xv))
918 ), ADD);
919 c = blendColor(c, color(
920 (lx.getBaseHuef() + 80 + p.fy/10) % 360,
921 constrain(140 - 2.2*abs(p.fy - model.yMax/2.), 0, 100),
922 max(0, ylv - ywv*abs(p.fy - yv))
923 ), ADD);
924 c = blendColor(c, color(
925 (lx.getBaseHuef() + 160 + p.fz / 10 + p.fy/2) % 360,
926 constrain(140 - 2.2*abs(p.fz - model.zMax/2.), 0, 100),
927 max(0, zlv - zwv*abs(p.fz - zv))
928 ), ADD);
929 colors[p.index] = c;
930 }
931 }
932 }
933
934 class Blinders extends SCPattern {
935
936 final SinLFO[] m;
937 final TriangleLFO r;
938 final SinLFO s;
939 final TriangleLFO hs;
940
941 public Blinders(GLucose glucose) {
942 super(glucose);
943 m = new SinLFO[12];
944 for (int i = 0; i < m.length; ++i) {
945 addModulator(m[i] = new SinLFO(0.5, 120, (120000. / (3+i)))).trigger();
946 }
947 addModulator(r = new TriangleLFO(9000, 15000, 29000)).trigger();
948 addModulator(s = new SinLFO(-20, 275, 11000)).trigger();
949 addModulator(hs = new TriangleLFO(0.1, 0.5, 15000)).trigger();
950 s.modulateDurationBy(r);
951 }
952
953 public void run(double deltaMs) {
954 float hv = lx.getBaseHuef();
955 int si = 0;
956 for (Strip strip : model.strips) {
957 int i = 0;
958 float mv = m[si % m.length].getValuef();
959 for (Point p : strip.points) {
960 colors[p.index] = color(
961 (hv + p.fz + p.fy*hs.getValuef()) % 360,
962 min(100, abs(p.fx - s.getValuef())/2.),
963 max(0, 100 - mv/2. - mv * abs(i - (strip.metrics.length-1)/2.))
964 );
965 ++i;
966 }
967 ++si;
968 }
969 }
970 }
971
972 class Psychedelia extends SCPattern {
973
974 final int NUM = 3;
975 SinLFO m = new SinLFO(-0.5, NUM-0.5, 9000);
976 SinLFO s = new SinLFO(-20, 147, 11000);
977 TriangleLFO h = new TriangleLFO(0, 240, 19000);
978 SinLFO c = new SinLFO(-.2, .8, 31000);
979
980 Psychedelia(GLucose glucose) {
981 super(glucose);
982 addModulator(m).trigger();
983 addModulator(s).trigger();
984 addModulator(h).trigger();
985 addModulator(c).trigger();
986 }
987
988 void run(double deltaMs) {
989 float huev = h.getValuef();
990 float cv = c.getValuef();
991 float sv = s.getValuef();
992 float mv = m.getValuef();
993 int i = 0;
994 for (Strip strip : model.strips) {
995 for (Point p : strip.points) {
996 colors[p.index] = color(
997 (huev + i*constrain(cv, 0, 2) + p.fz/2. + p.fx/4.) % 360,
998 min(100, abs(p.fy-sv)),
999 max(0, 100 - 50*abs((i%NUM) - mv))
1000 );
1001 }
1002 ++i;
1003 }
1004 }
1005 }
1006
1007 class AskewPlanes extends SCPattern {
1008
1009 class Plane {
1010 private final SinLFO a;
1011 private final SinLFO b;
1012 private final SinLFO c;
1013 float av = 1;
1014 float bv = 1;
1015 float cv = 1;
1016 float denom = 0.1;
1017
1018 Plane(int i) {
1019 addModulator(a = new SinLFO(-1, 1, 4000 + 1029*i)).trigger();
1020 addModulator(b = new SinLFO(-1, 1, 11000 - 1104*i)).trigger();
1021 addModulator(c = new SinLFO(-50, 50, 4000 + 1000*i * ((i % 2 == 0) ? 1 : -1))).trigger();
1022 }
1023
1024 void run(double deltaMs) {
1025 av = a.getValuef();
1026 bv = b.getValuef();
1027 cv = c.getValuef();
1028 denom = sqrt(av*av + bv*bv);
1029 }
1030 }
1031
1032 final Plane[] planes;
1033 final int NUM_PLANES = 3;
1034
1035 AskewPlanes(GLucose glucose) {
1036 super(glucose);
1037 planes = new Plane[NUM_PLANES];
1038 for (int i = 0; i < planes.length; ++i) {
1039 planes[i] = new Plane(i);
1040 }
1041 }
1042
1043 public void run(double deltaMs) {
1044 float huev = lx.getBaseHuef();
1045
1046 // This is super fucking bizarre. But if this is a for loop, the framerate
1047 // tanks to like 30FPS, instead of 60. Call them manually and it works fine.
1048 // Doesn't make ANY sense... there must be some weird side effect going on
1049 // with the Processing internals perhaps?
1050 // for (Plane plane : planes) {
1051 // plane.run(deltaMs);
1052 // }
1053 planes[0].run(deltaMs);
1054 planes[1].run(deltaMs);
1055 planes[2].run(deltaMs);
1056
1057 for (Point p : model.points) {
1058 float d = MAX_FLOAT;
1059 for (Plane plane : planes) {
1060 if (plane.denom != 0) {
1061 d = min(d, abs(plane.av*(p.fx-model.cx) + plane.bv*(p.fy-model.cy) + plane.cv) / plane.denom);
1062 }
1063 }
1064 colors[p.index] = color(
1065 (huev + abs(p.fx-model.cx)*.3 + p.fy*.8) % 360,
1066 max(0, 100 - .8*abs(p.fx - model.cx)),
1067 constrain(140 - 10.*d, 0, 100)
1068 );
1069 }
1070 }
1071 }
1072
1073 class ShiftingPlane extends SCPattern {
1074
1075 final SinLFO a = new SinLFO(-.2, .2, 5300);
1076 final SinLFO b = new SinLFO(1, -1, 13300);
1077 final SinLFO c = new SinLFO(-1.4, 1.4, 5700);
1078 final SinLFO d = new SinLFO(-10, 10, 9500);
1079
1080 ShiftingPlane(GLucose glucose) {
1081 super(glucose);
1082 addModulator(a).trigger();
1083 addModulator(b).trigger();
1084 addModulator(c).trigger();
1085 addModulator(d).trigger();
1086 }
1087
1088 public void run(double deltaMs) {
1089 float hv = lx.getBaseHuef();
1090 float av = a.getValuef();
1091 float bv = b.getValuef();
1092 float cv = c.getValuef();
1093 float dv = d.getValuef();
1094 float denom = sqrt(av*av + bv*bv + cv*cv);
1095 for (Point p : model.points) {
1096 float d = abs(av*(p.fx-model.cx) + bv*(p.fy-model.cy) + cv*(p.fz-model.cz) + dv) / denom;
1097 colors[p.index] = color(
1098 (hv + abs(p.fx-model.cx)*.6 + abs(p.fy-model.cy)*.9 + abs(p.fz - model.cz)) % 360,
1099 constrain(110 - d*6, 0, 100),
1100 constrain(130 - 7*d, 0, 100)
1101 );
1102 }
1103 }
1104 }
1105
1106 class Traktor extends SCPattern {
1107
1108 final int FRAME_WIDTH = 60;
1109
1110 final BasicParameter speed = new BasicParameter("SPD", 0.5);
1111
1112 private float[] bass = new float[FRAME_WIDTH];
1113 private float[] treble = new float[FRAME_WIDTH];
1114
1115 private int index = 0;
1116 private GraphicEQ eq = null;
1117
1118 public Traktor(GLucose glucose) {
1119 super(glucose);
1120 for (int i = 0; i < FRAME_WIDTH; ++i) {
1121 bass[i] = 0;
1122 treble[i] = 0;
1123 }
1124 addParameter(speed);
1125 }
1126
1127 public void onActive() {
1128 if (eq == null) {
1129 eq = new GraphicEQ(lx, 16);
1130 eq.slope.setValue(0.6);
1131 eq.level.setValue(0.65);
1132 eq.range.setValue(0.35);
1133 eq.release.setValue(0.4);
1134 addParameter(eq.level);
1135 addParameter(eq.range);
1136 addParameter(eq.attack);
1137 addParameter(eq.release);
1138 addParameter(eq.slope);
1139 }
1140 }
1141
1142 int counter = 0;
1143
1144 public void run(double deltaMs) {
1145 eq.run(deltaMs);
1146
1147 int stepThresh = (int) (40 - 39*speed.getValuef());
1148 counter += deltaMs;
1149 if (counter < stepThresh) {
1150 return;
1151 }
1152 counter = counter % stepThresh;
1153
1154 index = (index + 1) % FRAME_WIDTH;
1155
1156 float rawBass = eq.getAverageLevel(0, 4);
1157 float rawTreble = eq.getAverageLevel(eq.numBands-7, 7);
1158
1159 bass[index] = rawBass * rawBass * rawBass * rawBass;
1160 treble[index] = rawTreble * rawTreble;
1161
1162 for (Point p : model.points) {
1163 int i = (int) constrain((model.xMax - p.x) / model.xMax * FRAME_WIDTH, 0, FRAME_WIDTH-1);
1164 int pos = (index + FRAME_WIDTH - i) % FRAME_WIDTH;
1165
1166 colors[p.index] = color(
1167 (360 + lx.getBaseHuef() + .8*abs(p.x-model.cx)) % 360,
1168 100,
1169 constrain(9 * (bass[pos]*model.cy - abs(p.fy - model.cy)), 0, 100)
1170 );
1171 colors[p.index] = blendColor(colors[p.index], color(
1172 (400 + lx.getBaseHuef() + .5*abs(p.x-model.cx)) % 360,
1173 60,
1174 constrain(5 * (treble[pos]*.6*model.cy - abs(p.fy - model.cy)), 0, 100)
1175
1176 ), ADD);
1177 }
1178 }
1179 }
1180
1181 class ColorFuckerEffect extends SCEffect {
1182
1183 BasicParameter hueShift = new BasicParameter("HSHFT", 0);
1184 BasicParameter sat = new BasicParameter("SAT", 1);
1185 BasicParameter bright = new BasicParameter("BRT", 1);
1186 float[] hsb = new float[3];
1187
1188 ColorFuckerEffect(GLucose glucose) {
1189 super(glucose);
1190 addParameter(hueShift);
1191 addParameter(bright);
1192 addParameter(sat);
1193 }
1194
1195 public void doApply(int[] colors) {
1196 if (!enabled) {
1197 return;
1198 }
1199 float bMod = bright.getValuef();
1200 float sMod = sat.getValuef();
1201 float hMod = hueShift.getValuef();
1202 if (bMod < 1 || sMod < 1 || hMod > 0) {
1203 for (int i = 0; i < colors.length; ++i) {
1204 lx.RGBtoHSB(colors[i], hsb);
1205 colors[i] = lx.hsb(
1206 (360. * hsb[0] + hueShift.getValuef()*360.) % 360,
1207 100. * hsb[1] * sat.getValuef(),
1208 100. * hsb[2] * bright.getValuef()
1209 );
1210 }
1211 }
1212 }
1213 }
1214
1215 class BlurEffect extends SCEffect {
1216
1217 final LXParameter amount = new BasicParameter("AMT", 0);
1218 final int[] frame;
1219 final LinearEnvelope env = new LinearEnvelope(0, 1, 100);
1220
1221 BlurEffect(GLucose glucose) {
1222 super(glucose);
1223 addParameter(amount);
1224 addModulator(env);
1225 frame = new int[lx.total];
1226 for (int i = 0; i < frame.length; ++i) {
1227 frame[i] = #000000;
1228 }
1229 }
1230
1231 public void onEnable() {
1232 env.setRangeFromHereTo(1, 400).start();
1233 for (int i = 0; i < frame.length; ++i) {
1234 frame[i] = #000000;
1235 }
1236 }
1237
1238 public void onDisable() {
1239 env.setRangeFromHereTo(0, 1000).start();
1240 }
1241
1242 public void doApply(int[] colors) {
1243 float amt = env.getValuef() * amount.getValuef();
1244 if (amt > 0) {
1245 amt = (1 - amt);
1246 amt = 1 - (amt*amt*amt);
1247 for (int i = 0; i < colors.length; ++i) {
1248 // frame[i] = colors[i] = blendColor(colors[i], lerpColor(#000000, frame[i], amt, RGB), SCREEN);
1249 frame[i] = colors[i] = lerpColor(colors[i], blendColor(colors[i], frame[i], SCREEN), amt, RGB);
1250 }
1251 }
1252
1253 }
1254 }