MidiMusic pattern in progress and some framework changes
[SugarCubes.git] / MarkSlee.pde
index ee686b724eb6e6d26de63b6201d083312f940fa6..d501786c6fde06d672e0ec104b7f5d8194c6a42d 100644 (file)
@@ -1,3 +1,187 @@
+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);
@@ -999,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);
@@ -1016,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()
         );
       }
     }