Chills

import ddf.minim.*;
import ddf.minim.analysis.*;


Minim minim;
AudioPlayer groove;
BeatDetect beat;
BeatListener bl;


float[] sampL;

int dlimit = 200;
float samplebit;

boolean go = true;
int t = 0;
float wave;

float weight;

Attractor[] attractors;
Attractor dragged = null;

ArrayList dots;

PFont font;

void setup() {
  
  dots = new ArrayList();
  for ( int i = 0; i<dlimit; i++){
    dots.add(new Dot());
  }
  size(800, 800, P3D);
  samplebit = 0;

  minim = new Minim(this);
//  groove = minim.loadFile("smr.mp3", 2048);
//  groove = minim.loadFile("inthecoldstanding.mp3", 2048);
  groove = minim.loadFile("inthecold.wav", 2048);
  groove.play();
  //  sampL = new float[groove.left.size()];


  beat = new BeatDetect(groove.bufferSize(), groove.sampleRate());
  //frequency energy mode
//  beat = new BeatDetect();
  beat.setSensitivity(900);
  bl = new BeatListener(beat, groove);


  colorMode(HSB, 255);
  background(0);
  frameRate(12);
  stroke(255, 100);
  fill(255);
  ellipseMode(CENTER);

  font = loadFont("Verdana-11.vlw");
  textMode(SCREEN);
  textFont(font);

  //  smooth();

  makeAttractors();

  makeDots();
}

void makeAttractors() {


  // number of attractors to use
  int n = 16;
  int types = 14;

  attractors = new Attractor[n];
  float[] emittersx = new float[n];
  float[] emittersy = new float[n];


  for(int j=0; j<n; j++){
    //    emittersx[j] = random(width/2);
    //    emittersy[j] = random(height/2);
    emittersx[j] = random(width);
    emittersy[j] = random(height);
  }

  for(int i=0; i<n; i++){
    int t = floor(random(types+1));

    //    int t = 6;
    //0 - linear force from core
    //1 - heavy attractor
    //2 - outer rim attraction
    //3 - inner-core deflector
    //4 - boundary interaction
    //5 - squiggly swarm
    //6 - round-about
    //7 - round and down, then up
    //8 - gravity orbit
    //9 - slow orbit
    //10 - large orbit
    //11 - interesting deflector
    //12 - random firings
    //13 - linear orbit
    //14 - linear force from core

    float r = random(100,900);
    //    float r = 150;
    int col = round(lerp(0, 255, (float)groove.position()/groove.length()));
    //    color col = color(0,(round(lerp(0, 255, float(t)/float(types)))));
    attractors[i] = new Attractor(t, r, col);
    println(t);
    attractors[i].x = emittersx[i];
    attractors[i].y = emittersy[i];
  }

}

void makeDots(){





  for(int i=0; i<dlimit; i++){
    
    dots.add((Dot) new Dot());

  }


}

void draw() {
  println(frameRate);
  if(go){
    float timepos = ((float)groove.position()/groove.length());
    //sound sampling
    samplebit = groove.left.get((int)(random(groove.bufferSize()-1)));
    //    samplebit = random(.2);

    fill(map(groove.position(), 0,  groove.length(), 0, 255), 255, 255, 1) ;
    noStroke();
    rect(0, 0, width, height);

    for(int i=0; i<dots.size()-1; i++){
      // -----old-----------------------------
      //  dots[i].step(round(map(i, 0, dlimit, 1, 10)));
      //  dots[i].step(round(map(noise(random(100)), 0, 1, 1, 5)));
      //  dots[i].step(round(map(groove.get(round(map(i, 0, dlimit, 0, groove.length()-1)))), 0, 1, 1, 10));
      //  dots[i].step(map(groove.left.level(), 0, 1, 1, 10));
      //  dots[i].step(lerp(.1, 10, groove.left.level()/1));
      //  dots[i].setSampleBit(samplebit);

        Dot d;
        d = (Dot) dots.get(i);
        d.setSampleBit(samplebit);
        d.step(lerp(.1, 4, (float) groove.position() / groove.length()));
//        d.step(1);

    }

    oscillate(samplebit);



    //sound energy mode detection
//    if ( beat.isOnset() ) 
    //frequency mode detection
      if (beat.isKick() ) {

//      dlimit = int(map(noise(random(200.2)), 0, 1, 1, 10*timepos));
      dlimit = 10+int(100*timepos);

      makeDots();
      println("Max size = " +dlimit);
      println("Dots size = " + dots.size());
//      makeAttractors();

    }

  }
  saveFrame("screen-#####.tga");
  if ( !groove.isPlaying()){
    stop();
  }
}

void oscillate(float samplebit){

    t++;
  wave = samplebit + noise(t*0.01);
  //  wave = samplebit * .01;
}

void keyPressed(){ 
  switch(key){
  case ' ':
    //      go = !go;
    groove.skip(10000);
    break;
  case 's':
    save("img"+day()+nf(hour(),2)+nf(minute(),2)+nf(second(),2)+".tif");
    break;
  case 't':
    makeAttractors();
    makeDots();
    break;
  case 'r':
    makeDots();
    break;
  case 'f':
    println(dots);
    break;
  case 'd':
    dlimit = 10;
    makeDots();
    break;
  }

}




void stop(){
  groove.close();
  minim.stop();
  super.stop();
}

float easeIn (float t, float c) {
  return c*(t/=1)*t;
}

float easeOut (float t, float c) {
  return -c *(t/=1)*(t-2);
}

class Attractor {
  int type;
  float rad;
  float x, y;
  int col;
  float weight;
  
  //sound bit sample
  //float soundbit;
  

  Attractor(int type, float rad, int col) {
    this.type = type;
    this.rad = rad;
    this.col = col;
    this.weight = noise(random(100));
  
    //sound
    //this.soundbit = soundbit;

    //init x,y
    this.x = random(width/2);
    this.y = random(height/2);
  }

  float[] act(float _x, float _y, float _vx, float _vy) {
    float[] forces = new float[3];
    float dxy = dist(x, y, _x, _y);

    if(dxy < rad){
      float rx, ry;
      float dx = x - _x;
      float dy = y - _y;
      float ang = atan2(dy, dx);
      float ratio = 1 - easeOut(dxy/rad, 1);

      switch(type) {
      case 0:
      default:
        rx = _x < x ? -2 : 2;
        ry = _y < y ? -2 : 2;
        break;
      case 1:
        rx = (width/2 - _x)/100;
        ry = (height/2 - _y)/100;
        break;
      case 2:
        float radium = dxy - random(width*.2, height*.6);
        rx = radium/10 * sin(ang+HALF_PI);
        ry = -radium/10 * cos(ang+HALF_PI);
        break;
      case 3:
        rx = (wave*6-3) * sin(ang+HALF_PI);
        ry = -(wave*6-3) * cos(ang+HALF_PI);
        break;
      case 4:
        rx = -dx/50;
        ry = -dy/50;
        break;
      case 5:
        rx = random(-8, 8);
        ry = random(-8, 8);
        break;
      case 6:
        rx = 5 * sin(TWO_PI * wave);
        ry = -5 * cos(TWO_PI * wave);
        break;
      case 7:
        float _ang = atan2(_vy, _vx);
        rx = 3 * sin(_ang + PI*wave);
        ry = -3 * cos(_ang + PI*wave);
        break;
      case 8:
        rx = 5 * sin(ang);
        ry = -5 * cos(ang);
        break;
      case 9:
        rx = -5 * sin(ang + PI/12);
        ry = 5 * cos(ang + PI/12);
        break;
      case 10:
        rx = 5 * sin(ang - PI/12);
        ry = -5 * cos(ang - PI/16);
        break;
      case 11:
        rx = 5 * cos(ang);
        ry = -5 * sin(ang);
        break;
      case 12:
        rx = _vx * 2;
        ry = _vy * 2;
        break;
      case 13:
        rx = dy/20;
        ry = dx/20;
        break;
      }
      forces[0] = rx * ratio;
      forces[1] = ry * ratio;
      forces[2] = ratio;
    }
    return forces;
  }
  
  int getCol(){
    return col;
  }
  
  float getWeight(){
    return weight;
  }

}
class BeatListener implements AudioListener
{
  private BeatDetect beat;
  private AudioPlayer source;
  
  BeatListener(BeatDetect beat, AudioPlayer source)
  {
    this.source = source;
    this.source.addListener(this);
    this.beat = beat;
  }
  
  void samples(float[] samps)
  {
    beat.detect(source.mix);
  }
  
  void samples(float[] sampsL, float[] sampsR)
  {
    beat.detect(source.mix);
  }
}
class Dot {
  float x, y, ax, ay;
  float vx, vy, sx, sy;
  float flowx, flowy;
  float damp;
  float samplebit;

  Dot() {
    init();
    vx = 0;
    vy = 0;

    damp = .1*noise(samplebit);

  }

  void init() {
    ax = x = random(width);
    ay = y = random(height);
    flowx = random(-0.08, 0.08);
    flowy = random(-0.08, 0.08);
    samplebit = random(-1, 1);
//   flowx = random(-.01, .1);
//   flowy = random(.1*noise(random(1)), .01*noise(2)+noise(random(100)));
  }
  
  void setSampleBit(float samplebit){
     this.samplebit = samplebit;
     this.damp = .5*noise(samplebit);
  }
  
  

  void step(float lineweight) {
    
    float[] ratios = new float[attractors.length];
    float sr = map(random(10), 0, 10, 0, 1);
//    float sr = map(samplebit, -1, 1, 0, 1);

    for(int i=0; i<attractors.length; i++){
      
      float[] forces = attractors[i].act(x, y, vx, vy);
      sx += forces[0];
      sy += forces[1];
      ratios[i] = forces[2];
      sr += forces[2];
      
    }
    
    float col = 0;
    if(sr > 0){
      
      for(int j=0; j<attractors.length; j++){
        int acol = attractors[j].getCol();
//        float weight = map(attractors[j].getWeight(), 0, 1, 0, sr);
        float weight = norm(ratios[j], 0, sr);
        sr += .1;
        col += acol * weight; 
      
      }
      
    }

//old    
//    stroke(col, lerp(0, 255, sr), 255, 60);
//    stroke(col, lerp(0,255,.9), 255, map(noise(random(1200),random(200), random(500)), 0, 1, 5, 50));
//    stroke(constrain(col, 0, 255), 255, 255, constrain(t, 0, 100));
//    stroke(map(noise(col), 0, 1, 0,255), col, 255, map(noise(weight), 0, 1, 200, 255));

    stroke(lerp(0, 255, col/noise(col)), col, 255, map(noise(weight), 0, 1, 200, 255));
    strokeWeight(lineweight*1.2);
    vx += sx / attractors.length + flowx; 
    vy += sy / attractors.length + flowy;

    vx +=  constrain(samplebit, 0, .2);
    vy +=  map((float)groove.position() / groove.length(), 0, 1, -.2, .2);

    x += vx; 
    y += vy;
    line(ax, ay, x, y);

    ax = x; 
    ay = y;

    vx *= damp; 
    vy *= damp;

    sx = sy = 0;

    if (x < 0) ax = x = width;
    if (x > width) ax = x = 0;
    if (y < 1) ay = y = height;
    if (y > height-1) ay = y = 0;
  }
}

Stare 3 ½