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