cubecenter for cubecurl
[SugarCubes.git] / TobySegaran.pde
index 2eba4cfd5f74f324461b1e6dcd3d2c88d292494d..32b6d82aaa6a321a274e6196f167dc23d84d0f8b 100644 (file)
@@ -23,14 +23,14 @@ class GlitchPlasma extends SCPattern {
     }
   }
 
-  public void run(int deltaMs) {
+  public void run(double deltaMs) {
     for (Point p : model.points) {
       float hv = sin(dist(p.fx + pos, p.fy, 128.0, 128.0) / 8.0)
          + sin(dist(p.fx, p.fy, 64.0, 64.0) / 8.0)
          + sin(dist(p.fx, p.fy + pos / 7, 192.0, 64.0) / 7.0)
          + sin(dist(p.fx, p.fz + pos, 192.0, 100.0) / 8.0);
       float bv = 100;
-      colors[p.index] = color((hv+2)*25, satu, bv);
+      colors[p.index] = color((hv+2)*50, satu, bv);
     }
     if (random(1.0)<glitch/20) {
       pos=pos-int(random(10,30));
@@ -67,7 +67,7 @@ class FireEffect extends SCPattern {
     float br=min(100,sqrt(level)*15);
     return color(level/1.7,100,br);
   }
-  public void run(int deltaMs) {
+  public void run(double deltaMs) {
     for (int x=10;x<xm-10;x++) {
         if (x%50>45 || x%50<5) {
           intensity[x][ym-1] = random(30,100);
@@ -107,18 +107,18 @@ class StripBounce extends SCPattern {
       addModulator(fX[i]).trigger();      
       addModulator(fY[i]).trigger();
       addModulator(fZ[i]).trigger();
-      colorOffset[i]=random(0,100);
+      colorOffset[i]=random(0,256);
     }
   }
   
-  public void run(int deltaMs) {
+  public void run(double deltaMs) {
     float[] bright = new float[model.points.size()];
     for (Strip strip : model.strips) {
       for (int i=0;i<numOsc;i++) {
         float avgdist=0.0;
         avgdist = dist(strip.points.get(8).fx,strip.points.get(8).fy,strip.points.get(8).fz,fX[i].getValuef(),fY[i].getValuef(),fZ[i].getValuef());
         boolean on = avgdist<30;
-        float hv = (lx.getBaseHuef()+colorOffset[i])%100;
+        float hv = (lx.getBaseHuef()+colorOffset[i])%360;
         float br = max(0,100-avgdist*4);
         for (Point p : strip.points) {
           if (on && br>bright[p.index]) {
@@ -131,16 +131,29 @@ class StripBounce extends SCPattern {
   }
 }
 
-class SoundCubes extends SCPattern {
+class SoundRain extends SCPattern {
 
   private FFT fft = null; 
   private LinearEnvelope[] bandVals = null;
+  private float[] lightVals = null;
   private int avgSize;
-    
-  public SoundCubes(GLucose glucose) {
+  private float gain = 25;
+  SawLFO pos = new SawLFO(0, 9, 8000);
+  SinLFO col1 = new SinLFO(0, model.xMax, 5000);
+  BasicParameter gainParameter = new BasicParameter("GAIN", 0.5);
+  
+  public SoundRain(GLucose glucose) {
     super(glucose);
+    addModulator(pos).trigger();
+    addModulator(col1).trigger();
+    addParameter(gainParameter);
   }
 
+  public void onParameterChanged(LXParameter parameter) {
+    if (parameter == gainParameter) {
+      gain = 50*parameter.getValuef();
+    }
+  }
   protected void onActive() {
     if (this.fft == null) {
       this.fft = new FFT(lx.audioInput().bufferSize(), lx.audioInput().sampleRate());
@@ -151,18 +164,139 @@ class SoundCubes extends SCPattern {
       for (int i = 0; i < this.bandVals.length; ++i) {
         this.addModulator(this.bandVals[i] = (new LinearEnvelope(0, 0, 700+i*4))).trigger();
       }
+      lightVals = new float[avgSize];
     }
   }
   
-  public void run(int deltaMs) {
+  public void run(double deltaMs) {
     this.fft.forward(this.lx.audioInput().mix);
+    for (int i = 0; i < avgSize; ++i) {
+      float value = this.fft.getAvg(i);
+      this.bandVals[i].setEndVal(value,40).trigger();
+      float lv = min(value*gain,100);
+      if (lv>lightVals[i]) {
+        lightVals[i]=min(lightVals[i]+15,lv,100);
+      } else {
+        lightVals[i]=max(lv,lightVals[i]-5,0);
+      }
+    }
+    for (Cube c : model.cubes) {
+      for (int j=0; j<c.strips.size(); j++) {
+        Strip s = c.strips.get(j);
+        if (j%4!=0 && j%4!=2) {
+          for (Point p : s.points) {
+            int seq = int(p.fy*avgSize/model.yMax+pos.getValuef()+sin(p.fx+p.fz)*2)%avgSize;
+            seq=min(abs(seq-(avgSize/2)),avgSize-1);
+            colors[p.index] = color(200,max(0,100-abs(p.fx-col1.getValuef())/2),lightVals[seq]);
+          }
+        }
+      }
+    }
+  }  
+}
+
+class FaceSync extends SCPattern {
+  SinLFO xosc = new SinLFO(-10, 10, 3000);
+  SinLFO zosc = new SinLFO(-10, 10, 3000);
+  SinLFO col1 = new SinLFO(0, model.xMax, 5000);
+  SinLFO col2 = new SinLFO(0, model.xMax, 4000);
+
+  public FaceSync(GLucose glucose) {
+    super(glucose);
+    addModulator(xosc).trigger();
+    addModulator(zosc).trigger();
+    zosc.setValue(0);
+    addModulator(col1).trigger();
+    addModulator(col2).trigger();    
+    col2.setValue(model.xMax);
+  }
+
+  public void run(double deltaMs) {
+    int i=0;
+    for (Strip s : model.strips) {
+      i++;
+      for (Point p : s.points) {
+        float dx, dz;
+        if (i%32 < 16) {
+          dx = p.fx - (s.cx+xosc.getValuef());
+          dz = p.fz - (s.cz+zosc.getValuef());
+        } else {
+          dx = p.fx - (s.cx+zosc.getValuef());
+          dz = p.fz - (s.cz+xosc.getValuef());
+        }                
+        //println(dx);
+        float a1=max(0,100-abs(p.fx-col1.getValuef()));
+        float a2=max(0,100-abs(p.fx-col2.getValuef()));        
+        float sat = max(a1,a2);
+        float h = (359*a1+200*a2) / (a1+a2);
+        colors[p.index] = color(h,sat,100-abs(dx*5)-abs(dz*5));
+      }
+    }
+  }
+}
+
+class SoundSpikes extends SCPattern {
+  private FFT fft = null; 
+  private LinearEnvelope[] bandVals = null;
+  private float[] lightVals = null;
+  private int avgSize;
+  private float gain = 25;
+  BasicParameter gainParameter = new BasicParameter("GAIN", 0.5);
+  SawLFO pos = new SawLFO(0, model.xMax, 8000);
 
+  public SoundSpikes(GLucose glucose) {
+    super(glucose);
+    addParameter(gainParameter);
+    addModulator(pos).trigger();
+  }
+
+  public void onParameterChanged(LXParameter parameter) {
+    if (parameter == gainParameter) {
+      gain = 50*parameter.getValuef();
+    }
+  }
+  protected void onActive() {
+    if (this.fft == null) {
+      this.fft = new FFT(lx.audioInput().bufferSize(), lx.audioInput().sampleRate());
+      this.fft.window(FFT.HAMMING);
+      this.fft.logAverages(40, 1);
+      this.avgSize = this.fft.avgSize();
+      this.bandVals = new LinearEnvelope[this.avgSize];
+      for (int i = 0; i < this.bandVals.length; ++i) {
+        this.addModulator(this.bandVals[i] = (new LinearEnvelope(0, 0, 700+i*4))).trigger();
+      }
+      lightVals = new float[avgSize];
+    }
+  }
+  
+  public void run(double deltaMs) {
+    this.fft.forward(this.lx.audioInput().mix);
     for (int i = 0; i < avgSize; ++i) {
       float value = this.fft.getAvg(i);
       this.bandVals[i].setEndVal(value,40).trigger();
+      float lv = min(value*gain,model.yMax+10);
+      if (lv>lightVals[i]) {
+        lightVals[i]=min(lightVals[i]+30,lv,model.yMax+10);
+      } else {
+        lightVals[i]=max(lv,lightVals[i]-10,0);
+      }
     }
-    for (Point p : model.points) {
-      colors[p.index] = color(100,0,bandVals[1].getValuef()*25 );
+    int i = 0;
+    for (Cube c : model.cubes) {
+      for (int j=0; j<c.strips.size(); j++) {
+        Strip s = c.strips.get(j);
+        if (j%4!=0 && j%4!=2) {
+          for (Point p : s.points) {
+            float dis = (abs(p.fx-model.xMax/2)+pos.getValuef())%model.xMax/2;
+            int seq = int((dis*avgSize*2)/model.xMax);
+            if (seq>avgSize) seq=avgSize-seq;
+            seq=constrain(seq,0,avgSize-1);
+            float br=max(0, lightVals[seq]-p.fy);
+            colors[p.index] = color((dis*avgSize*65)/model.xMax,90,br);
+          }
+        }
+      }
     }
   }  
 }
+