MidiMusic pattern in progress and some framework changes
[SugarCubes.git] / MarkSlee.pde
index 699df38d540d5fb9cfa391b825bf4e636f253415..d501786c6fde06d672e0ec104b7f5d8194c6a42d 100644 (file)
@@ -1,3 +1,315 @@
+class MidiMusic extends SCPattern {
+  
+  private final Map<Integer, LightUp> lightMap = new HashMap<Integer, LightUp>();
+  private final List<LightUp> allLights = new ArrayList<LightUp>();
+  
+  private final Stack<LightUp> newLayers = new Stack<LightUp>();
+  
+  MidiMusic(GLucose glucose) {
+    super(glucose);
+  }
+  
+  class LightUp extends LXLayer {
+    
+    private LinearEnvelope brt = new LinearEnvelope(0, 0, 0);
+    private 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() - 64) / 12., 0, 1));
+      yPos.setValue(lerp(20, model.yMax, note.getVelocity() / 127.));
+      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 b = max(0, bVal - 3*dist(p.x, p.y, xPos, yVal));
+        if (b > 0) {
+          colors[p.index] = blendColor(colors[p.index], color(
+            (lx.getBaseHuef() + abs(p.x - model.cx) + abs(p.y - model.cy)) % 360,
+            100,
+            b
+          ), ADD);
+        }
+      }
+    }
+  }
+  
+  public synchronized boolean noteOnReceived(Note note) {
+    if (note.getChannel() == 0) {
+      for (LightUp light : allLights) {
+        if (light.isAvailable()) {
+          light.noteOn(note);
+          lightMap.put(note.getPitch(), light);
+          return true;
+        }
+      }
+      LightUp newLight = new LightUp();
+      newLight.noteOn(note);
+      lightMap.put(note.getPitch(), newLight);
+      synchronized(newLayers) {
+        newLayers.push(newLight);
+      }
+    } else if (note.getChannel() == 1) {
+    }
+    return true;
+  }
+  
+  public synchronized boolean noteOffReceived(Note note) {
+    if (note.getChannel() == 0) {
+      LightUp light = lightMap.get(note.getPitch());
+      if (light != null) {
+        light.noteOff(note);
+      }
+    }
+    return true;
+  }
+  
+  public synchronized void run(double deltaMs) {
+    setColors(#000000);
+    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.1);
+  
+  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(40, 50), 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();
+    }
+    int j = 0;
+    for (Click d : delays) {
+      if (d.click()) {
+        gravity[j].start();
+        d.stop();
+      }
+      ++j;
+    }   
+    
+    for (Accelerator g : gravity) {
+      if (isRising) {
+        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 {
+        if (g.getValuef() < 0) {
+          g.setValue(-g.getValuef());
+          g.setVelocity(-g.getVelocityf() * random(0.74, 0.84));
+        }
+      }
+    }
+    
+    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 i = (int) constrain((p.x - model.xMin) * NUM_DIVISIONS / (model.xMax - model.xMin), 0, NUM_DIVISIONS-1);
+      colors[p.index] = color(
+        (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[i].getValuef())*falloff)
+      );
+    }
+  }
+}
+
+class ViolinWave extends SCPattern {
+  
+  BasicParameter level = new BasicParameter("LVL", 0.45);
+  BasicParameter range = new BasicParameter("RNG", 0.5);
+  BasicParameter edge = new BasicParameter("EDG", 0.5);
+  BasicParameter release = new BasicParameter("RLS", 0.5);
+  BasicParameter speed = new BasicParameter("SPD", 0.5);
+  BasicParameter amp = new BasicParameter("AMP", 0.25);
+  BasicParameter period = new BasicParameter("WAVE", 0.5);
+  BasicParameter pSize = new BasicParameter("PSIZE", 0.5);
+  BasicParameter pSpeed = new BasicParameter("PSPD", 0.5);
+  BasicParameter pDensity = new BasicParameter("PDENS", 0.25);
+  
+  LinearEnvelope dbValue = new LinearEnvelope(0, 0, 10);
+
+  ViolinWave(GLucose glucose) {
+    super(glucose);
+    addParameter(level);
+    addParameter(edge);
+    addParameter(range);
+    addParameter(release);
+    addParameter(speed);
+    addParameter(amp);
+    addParameter(period);
+    addParameter(pSize);
+    addParameter(pSpeed);
+    addParameter(pDensity);
+
+    addModulator(dbValue);
+  }
+  
+  final List<Particle> particles = new ArrayList<Particle>();
+  
+  class Particle {
+    
+    LinearEnvelope x = new LinearEnvelope(0, 0, 0);
+    LinearEnvelope y = new LinearEnvelope(0, 0, 0);
+    
+    Particle() {
+      addModulator(x);
+      addModulator(y);
+    }
+    
+    Particle trigger(boolean direction) {
+      float xInit = random(model.xMin, model.xMax);
+      float time = 3000 - 2500*pSpeed.getValuef();
+      x.setRange(xInit, xInit + random(-40, 40), time).trigger();
+      y.setRange(model.cy + 10, direction ? model.yMax + 50 : model.yMin - 50, time).trigger();
+      return this;
+    }
+    
+    boolean isActive() {
+      return x.isRunning() || y.isRunning();
+    }
+    
+    public void run(double deltaMs) {
+      if (!isActive()) {
+        return;
+      }
+      
+      float pFalloff = (30 - 27*pSize.getValuef());
+      for (Point p : model.points) {
+        float b = 100 - pFalloff * (abs(p.x - x.getValuef()) + abs(p.y - y.getValuef()));
+        if (b > 0) {
+          colors[p.index] = blendColor(colors[p.index], color(
+            lx.getBaseHuef(), 20, b
+          ), ADD);
+        }
+      }
+    }
+  }
+  
+  float[] centers = new float[30];
+  double accum = 0;
+  boolean rising = true;
+  
+  void fireParticle(boolean direction) {
+    boolean gotOne = false;
+    for (Particle p : particles) {
+      if (!p.isActive()) {
+       p.trigger(direction);
+       return;
+      }
+    }
+    particles.add(new Particle().trigger(direction));
+  }
+  
+  public void run(double deltaMs) {
+    accum += deltaMs / (1000. - 900.*speed.getValuef());
+    for (int i = 0; i < centers.length; ++i) {
+      centers[i] = model.cy + 30*amp.getValuef()*sin((float) (accum + (i-centers.length/2.)/(1. + 9.*period.getValuef())));
+    }
+    
+    float zeroDBReference = pow(10, (50 - 190*level.getValuef())/20.);
+    float dB = 20*GraphicEQ.log10(lx.audioInput().mix.level() / zeroDBReference);    
+    if (dB > dbValue.getValuef()) {
+      rising = true;
+      dbValue.setRangeFromHereTo(dB, 10).trigger();
+    } else {
+      if (rising) {
+        for (int j = 0; j < pDensity.getValuef()*3; ++j) {
+          fireParticle(true);
+          fireParticle(false);
+        }
+      }
+      rising = false;
+      dbValue.setRangeFromHereTo(max(dB, -96), 50 + 1000*release.getValuef()).trigger();
+    }
+    float edg = 1 + edge.getValuef() * 40;
+    float rng = (78 - 64 * range.getValuef()) / (model.yMax - model.cy);
+    float val = max(2, dbValue.getValuef());
+    
+    for (Point p : model.points) {
+      int ci = (int) lerp(0, centers.length-1, (p.x - model.xMin) / (model.xMax - model.xMin));
+      float rFactor = 1.0 -  0.9 * abs(p.x - model.cx) / (model.xMax - model.cx);
+      colors[p.index] = color(
+        (lx.getBaseHuef() + abs(p.x - model.cx)) % 360,
+        min(100, 20 + 8*abs(p.y - centers[ci])),
+        constrain(edg*(val*rFactor - rng * abs(p.y-centers[ci])), 0, 100)
+      );
+    }
+    
+    for (Particle p : particles) {
+      p.run(deltaMs);
+    }
+  }
+}
+
 class BouncyBalls extends SCPattern {
   
   static final int NUM_BALLS = 6;
@@ -871,6 +1183,7 @@ class ColorFuckerEffect extends SCEffect {
   BasicParameter hueShift = new BasicParameter("HSHFT", 0);
   BasicParameter sat = new BasicParameter("SAT", 1);  
   BasicParameter bright = new BasicParameter("BRT", 1);
+  float[] hsb = new float[3];
   
   ColorFuckerEffect(GLucose glucose) {
     super(glucose);
@@ -888,10 +1201,11 @@ class ColorFuckerEffect extends SCEffect {
     float hMod = hueShift.getValuef();
     if (bMod < 1 || sMod < 1 || hMod > 0) {    
       for (int i = 0; i < colors.length; ++i) {
-        colors[i] = color(
-          (hue(colors[i]) + hueShift.getValuef()*360.) % 360,
-          saturation(colors[i]) * sat.getValuef(),
-          brightness(colors[i]) * bright.getValuef()
+        lx.RGBtoHSB(colors[i], hsb);
+        colors[i] = lx.hsb(
+          (360. * hsb[0] + hueShift.getValuef()*360.) % 360,
+          100. * hsb[1] * sat.getValuef(),
+          100. * hsb[2] * bright.getValuef()
         );
       }
     }