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