Merge branch 'master' of https://github.com/sugarcubes/SugarCubes
[SugarCubes.git] / MarkSlee.pde
index 81b26299a013374b4b8d6a42f22fd119d0bab6d1..58b0ea66f1f876c033396951149ce9062b30643f 100644 (file)
@@ -1,18 +1,55 @@
 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> allLights = new ArrayList<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 Stack<LightUp> newLayers = new Stack<LightUp>();
+  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 LinearEnvelope brt = new LinearEnvelope(0, 0, 0);
-    private Accelerator yPos = new Accelerator(0, 0, 0);
+    private final LinearEnvelope brt = new LinearEnvelope(0, 0, 0);
+    private final Accelerator yPos = new Accelerator(0, 0, 0);
     private float xPos;
     
     LightUp() {
@@ -25,8 +62,8 @@ class MidiMusic extends SCPattern {
     }
     
     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.));
+      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();     
     }
 
@@ -42,10 +79,11 @@ class MidiMusic extends SCPattern {
       }
       float yVal = yPos.getValuef();
       for (Point p : model.points) {
-        float b = max(0, bVal - 3*dist(p.x, p.y, xPos, yVal));
+        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() + abs(p.x - model.cx) + abs(p.y - model.cy)) % 360,
+            (lx.getBaseHuef() + .2*abs(p.x - model.cx) + .2*abs(p.y - model.cy)) % 360,
             100,
             b
           ), ADD);
@@ -54,22 +92,61 @@ class MidiMusic extends SCPattern {
     }
   }
   
+  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) {
-      for (LightUp light : allLights) {
-        if (light.isAvailable()) {
-          light.noteOn(note);
-          lightMap.put(note.getPitch(), light);
-          return true;
+      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;
         }
       }
-      LightUp newLight = new LightUp();
-      newLight.noteOn(note);
-      lightMap.put(note.getPitch(), newLight);
-      synchronized(newLayers) {
-        newLayers.push(newLight);
-      }
-    } else if (note.getChannel() == 1) {
     }
     return true;
   }
@@ -84,8 +161,30 @@ class MidiMusic extends SCPattern {
     return true;
   }
   
+  final float[] wval = new float[16];
+  float wavoff = 0;
+  
   public synchronized void run(double deltaMs) {
-    setColors(#000000);
+    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()) {
@@ -173,11 +272,13 @@ class Pulley extends SCPattern {
     }
 
     // A little silliness to test the grid API    
-    for (int i = 0; i < 7; ++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)/7. - gravity[gi].getValuef() / model.yMax);
-        midiEngine.grid.setState(i, j, (b < 0) ? 0 : 1);
+    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);
+        }
       }
     }
     
@@ -1199,6 +1300,7 @@ class ColorFuckerEffect extends SCEffect {
   final BasicParameter desat = new BasicParameter("DSAT", 0);
   final BasicParameter sharp = new BasicParameter("SHARP", 0);
   final BasicParameter soft = new BasicParameter("SOFT", 0);
+  final BasicParameter mono = new BasicParameter("MONO", 0);
   final BasicParameter invert = new BasicParameter("INVERT", 0);
   final BasicParameter hueShift = new BasicParameter("HSHFT", 0);
   
@@ -1210,6 +1312,7 @@ class ColorFuckerEffect extends SCEffect {
     addParameter(desat);
     addParameter(sharp);
     addParameter(soft);
+    addParameter(mono);
     addParameter(invert);
     addParameter(hueShift);
   }
@@ -1223,10 +1326,14 @@ class ColorFuckerEffect extends SCEffect {
     float hMod = hueShift.getValuef();
     float fSharp = 1/(1.0001-sharp.getValuef());
     float fSoft = soft.getValuef();
+    boolean mon = mono.getValuef() > 0.5;
     boolean ivt = invert.getValuef() > 0.5;
-    if (bMod < 1 || sMod < 1 || hMod > 0 || fSharp > 0 || ivt || fSoft > 0) {
+    if (bMod < 1 || sMod < 1 || hMod > 0 || fSharp > 0 || ivt || mon || fSoft > 0) {
       for (int i = 0; i < colors.length; ++i) {
         lx.RGBtoHSB(colors[i], hsb);
+        if (mon) {
+          hsb[0] = lx.getBaseHuef() / 360.;
+        }
         if (ivt) {
           hsb[2] = 1 - hsb[2];
         }