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