+class MidiMusic extends SCPattern {
+
+ private final Stack<LXLayer> newLayers = new Stack<LXLayer>();
+
+ private final Map<Integer, LightUp> lightMap = new HashMap<Integer, LightUp>();
+ private final List<LightUp> lights = new ArrayList<LightUp>();
+ private final BasicParameter lightSize = new BasicParameter("SIZE", 0.5);
+
+ private final List<Sweep> sweeps = new ArrayList<Sweep>();
+
+ private final LinearEnvelope sparkle = new LinearEnvelope(0, 1, 500);
+ private boolean sparkleDirection = true;
+ private float sparkleBright = 100;
+
+ private final BasicParameter wave = new BasicParameter("WAVE", 0);
+
+ MidiMusic(GLucose glucose) {
+ super(glucose);
+ addParameter(lightSize);
+ addParameter(wave);
+ addModulator(sparkle).setValue(1);
+ }
+
+ class Sweep extends LXLayer {
+
+ final LinearEnvelope position = new LinearEnvelope(0, 1, 1000);
+ float bright = 100;
+ float falloff = 10;
+
+ Sweep() {
+ addModulator(position);
+ }
+
+ public void run(double deltaMs, color[] colors) {
+ if (!position.isRunning()) {
+ return;
+ }
+ float posf = position.getValuef();
+ for (Point p : model.points) {
+ colors[p.index] = blendColor(colors[p.index], color(
+ (lx.getBaseHuef() + .2*abs(p.x - model.cx) + .2*abs(p.y - model.cy)) % 360,
+ 100,
+ max(0, bright - posf*100 - falloff*abs(p.y - posf*model.yMax))
+ ), ADD);
+ }
+ }
+ }
+
+ class LightUp extends LXLayer {
+
+ private final LinearEnvelope brt = new LinearEnvelope(0, 0, 0);
+ private final Accelerator yPos = new Accelerator(0, 0, 0);
+ private float xPos;
+
+ LightUp() {
+ addModulator(brt);
+ addModulator(yPos);
+ }
+
+ boolean isAvailable() {
+ return brt.getValuef() <= 0;
+ }
+
+ void noteOn(Note note) {
+ xPos = lerp(0, model.xMax, constrain(0.5 + (note.getPitch() - 60) / 28., 0, 1));
+ yPos.setValue(lerp(20, model.yMax*.72, note.getVelocity() / 127.)).stop();
+ brt.setRangeFromHereTo(lerp(40, 100, note.getVelocity() / 127.), 20).start();
+ }
+
+ void noteOff(Note note) {
+ yPos.setVelocity(0).setAcceleration(-380).start();
+ brt.setRangeFromHereTo(0, 1000).start();
+ }
+
+ public void run(double deltaMs, color[] colors) {
+ float bVal = brt.getValuef();
+ if (bVal <= 0) {
+ return;
+ }
+ float yVal = yPos.getValuef();
+ for (Point p : model.points) {
+ float falloff = 6 - 5*lightSize.getValuef();
+ float b = max(0, bVal - falloff*dist(p.x, p.y, xPos, yVal));
+ if (b > 0) {
+ colors[p.index] = blendColor(colors[p.index], lx.hsb(
+ (lx.getBaseHuef() + .2*abs(p.x - model.cx) + .2*abs(p.y - model.cy)) % 360,
+ 100,
+ b
+ ), ADD);
+ }
+ }
+ }
+ }
+
+ private LightUp getLight() {
+ for (LightUp light : lights) {
+ if (light.isAvailable()) {
+ return light;
+ }
+ }
+ LightUp newLight = new LightUp();
+ lights.add(newLight);
+ synchronized(newLayers) {
+ newLayers.push(newLight);
+ }
+ return newLight;
+ }
+
+ private Sweep getSweep() {
+ for (Sweep s : sweeps) {
+ if (!s.position.isRunning()) {
+ return s;
+ }
+ }
+ Sweep newSweep = new Sweep();
+ sweeps.add(newSweep);
+ synchronized(newLayers) {
+ newLayers.push(newSweep);
+ }
+ return newSweep;
+ }
+
+ public synchronized boolean noteOn(Note note) {
+ if (note.getChannel() == 0) {
+ LightUp light = getLight();
+ lightMap.put(note.getPitch(), light);
+ light.noteOn(note);
+ } else if (note.getChannel() == 1) {
+ } else if (note.getChannel() == 9) {
+ if (note.getVelocity() > 0) {
+ switch (note.getPitch()) {
+ case 36:
+ Sweep s = getSweep();
+ s.bright = 50 + note.getVelocity() / 127. * 50;
+ s.falloff = 20 - note.getVelocity() / 127. * 17;
+ s.position.trigger();
+ break;
+ case 37:
+ sparkleBright = note.getVelocity() / 127. * 100;
+ sparkleDirection = true;
+ sparkle.trigger();
+ break;
+ case 38:
+ sparkleBright = note.getVelocity() / 127. * 100;
+ sparkleDirection = false;
+ sparkle.trigger();
+ break;
+ }
+ }
+ }
+ return true;
+ }
+
+ public synchronized boolean noteOff(Note note) {
+ if (note.getChannel() == 0) {
+ LightUp light = lightMap.get(note.getPitch());
+ if (light != null) {
+ light.noteOff(note);
+ }
+ }
+ return true;
+ }
+
+ final float[] wval = new float[16];
+ float wavoff = 0;
+
+ public synchronized void run(double deltaMs) {
+ wavoff += deltaMs * .001;
+ for (int i = 0; i < wval.length; ++i) {
+ wval[i] = model.cy + 0.2 * model.yMax/2. * sin(wavoff + i / 1.9);
+ }
+ float sparklePos = (sparkleDirection ? sparkle.getValuef() : (1 - sparkle.getValuef())) * (Cube.POINTS_PER_STRIP)/2.;
+ float maxBright = sparkleBright * (1 - sparkle.getValuef());
+ for (Strip s : model.strips) {
+ int i = 0;
+ for (Point p : s.points) {
+ int wavi = (int) constrain(p.x / model.xMax * wval.length, 0, wval.length-1);
+ float wavb = max(0, wave.getValuef()*100. - 8.*abs(p.y - wval[wavi]));
+ colors[p.index] = color(
+ (lx.getBaseHuef() + .2*abs(p.x - model.cx) + .2*abs(p.y - model.cy)) % 360,
+ 100,
+ constrain(wavb + max(0, maxBright - 40.*abs(sparklePos - abs(i - (Cube.POINTS_PER_STRIP-1)/2.))), 0, 100)
+ );
+ ++i;
+ }
+ }
+
+ if (!newLayers.isEmpty()) {
+ synchronized(newLayers) {
+ while (!newLayers.isEmpty()) {
+ addLayer(newLayers.pop());
+ }
+ }
+ }
+ }
+}
+
+class Pulley extends SCPattern {
+
+ final int NUM_DIVISIONS = 16;
+ private final Accelerator[] gravity = new Accelerator[NUM_DIVISIONS];
+ private final Click[] delays = new Click[NUM_DIVISIONS];
+
+ private final Click reset = new Click(9000);
+ private boolean isRising = false;
+
+ private BasicParameter sz = new BasicParameter("SIZE", 0.5);
+ private BasicParameter beatAmount = new BasicParameter("BEAT", 0);
+
+ Pulley(GLucose glucose) {
+ super(glucose);
+ for (int i = 0; i < NUM_DIVISIONS; ++i) {
+ addModulator(gravity[i] = new Accelerator(0, 0, 0));
+ addModulator(delays[i] = new Click(0));
+ }
+ addModulator(reset).start();
+ addParameter(sz);
+ addParameter(beatAmount);
+ trigger();
+
+ }
+
+ private void trigger() {
+ isRising = !isRising;
+ int i = 0;
+ for (Accelerator g : gravity) {
+ if (isRising) {
+ g.setSpeed(random(20, 33), 0).start();
+ } else {
+ g.setVelocity(0).setAcceleration(-420);
+ delays[i].setDuration(random(0, 500)).trigger();
+ }
+ ++i;
+ }
+ }
+
+ public void run(double deltaMs) {
+ if (reset.click()) {
+ trigger();
+ }
+
+ if (isRising) {
+ // Fucking A, had to comment this all out because of that bizarre
+ // Processing bug where some simple loop takes an absurd amount of
+ // time, must be some pre-processor bug
+// for (Accelerator g : gravity) {
+// if (g.getValuef() > model.yMax) {
+// g.stop();
+// } else if (g.getValuef() > model.yMax*.55) {
+// if (g.getVelocityf() > 10) {
+// g.setAcceleration(-16);
+// } else {
+// g.setAcceleration(0);
+// }
+// }
+// }
+ } else {
+ int j = 0;
+ for (Click d : delays) {
+ if (d.click()) {
+ gravity[j].start();
+ d.stop();
+ }
+ ++j;
+ }
+ for (Accelerator g : gravity) {
+ if (g.getValuef() < 0) {
+ g.setValue(-g.getValuef());
+ g.setVelocity(-g.getVelocityf() * random(0.74, 0.84));
+ }
+ }
+ }
+
+ // A little silliness to test the grid API
+ if (midiEngine != null && midiEngine.getFocusedPattern() == this) {
+ for (int i = 0; i < 5; ++i) {
+ for (int j = 0; j < 8; ++j) {
+ int gi = (int) constrain(j * NUM_DIVISIONS / 8, 0, NUM_DIVISIONS-1);
+ float b = 1 - 4.*abs((6-i)/6. - gravity[gi].getValuef() / model.yMax);
+ midiEngine.grid.setState(i, j, (b < 0) ? 0 : 3);
+ }
+ }
+ }
+
+ float fPos = 1 - lx.tempo.rampf();
+ if (fPos < .2) {
+ fPos = .2 + 4 * (.2 - fPos);
+ }
+ float falloff = 100. / (3 + sz.getValuef() * 36 + fPos * beatAmount.getValuef()*48);
+ for (Point p : model.points) {
+ int gi = (int) constrain((p.x - model.xMin) * NUM_DIVISIONS / (model.xMax - model.xMin), 0, NUM_DIVISIONS-1);
+ colors[p.index] = lx.hsb(
+ (lx.getBaseHuef() + abs(p.x - model.cx)*.8 + p.y*.4) % 360,
+ constrain(130 - p.y*.8, 0, 100),
+ max(0, 100 - abs(p.y - gravity[gi].getValuef())*falloff)
+ );
+ }
+ }
+}
+