Monday, 6 October 2014

Testing code using 2nd song

This is a video recording the improved code, using the second song as reference to compare to the visualization generated from the first. I hope I am not going crazy looking at all these dancing points and they really are different!
import ddf.minim.analysis.*;
import ddf.minim.*;

Minim minim;
AudioPlayer [] players = new AudioPlayer[2];;
int
  samples = 1024;
float
  angleStep  = TWO_PI/samples,
  maxRadius = 300.,
  scaleF = 125.,
  radiusStep;
PVector
  shapeCenter,
  currentVertex;
PVector[]
  lookupTable;

float phasorInc = samples / 53030.0;
float phasorInc2 = samples / 90000.0;
int nReflections = 10;
int nPointsPerFrame = 3600;
int nAngles = 6;

Walker w;
Phasor p;
Phasor p2;
float[] angles;
Palette palette;

PVector getVCoordinates(PVector v, float d, float a) {
  return new PVector(v.x + d * cos(a), v.y + d * sin(a));
}

float getAngleFromCenter(PVector v) {
  return atan2(v.y - height / 2, v.x - width / 2);
}

class Phasor {
  float inc;
  float phase;

  Phasor(float inc) {
    this.inc = inc;
  }

  void update() {
    phase += inc;

    while (phase >= 1.0) {
      phase -= 1.0;
    }
    while (phase < 0) {
      phase += 1.0;
    }
  }
}

class Walker {
  PVector v;
 
  Walker(float x, float y) {
    v = new PVector(x, y);
  }
 
  void update() {
    float gauss = p2.phase * random(2);
    v = getVCoordinates(v, gauss, angles[int(random(angles.length))]);
 
    float offset = width / 4;

    if (v.x >= width + offset) {
      v.x -= width + offset;
    }
   
    if (v.x < -offset) {
      v.x += width + offset;
    }
   
    if (v.y >= height + offset) {
      v.y -= height + offset;
    }
   
    if (v.y < 0) {
      v.y += height + offset;
    }
  
    float a = getAngleFromCenter(v);
    float d = dist(v.x, v.y, width / 2, height / 2);
    PVector center = new PVector(width / 2, height / 2);
    float rAngle = PI / (float) nReflections;
    noStroke();
    fill(palette.getNorm(p.phase), map(d, 0, width, 64, 12));
    gauss = max(0.5, gauss / 2);

    for (int i = 0; i < nReflections; i++) {
      float thisAngle = a + (TWO_PI / (float) nReflections) * i;

      PVector thisV = getVCoordinates(center, d, thisAngle);
      ellipse(thisV.x, thisV.y, gauss, gauss);

      thisV = getVCoordinates(center, d, PI - thisAngle);
      ellipse(thisV.x, thisV.y, gauss, gauss);
    }
  }
}

class Palette {
  ArrayList<Integer> colors;

  Palette() {
    colors = new ArrayList<Integer>();
  }

  void add(color c) {
    colors.add(c);
  }

  color getNorm(float p) {
    int index = (int) (p * colors.size());
    color c1 = colors.get(index);
    color c2 = colors.get((index + 1) % colors.size());

    return lerpColor(c1, c2, p * colors.size() - index);
  }
}


void setup()
{
  size(1000, 1000, P3D);
 
    angles = new float[nAngles];
  for (int i = 0; i < angles.length; i++) {
    angles[i] = ((float) i / (float) nAngles) * TWO_PI;
  }

  w = new Walker(width / 2, height / 2);
  p = new Phasor(phasorInc);
  p2 = new Phasor(phasorInc2);

  palette = new Palette();
    palette.add(color(11, 17, 13));
  palette.add(color(44, 77, 86));
  palette.add(color(195, 170, 114));
  palette.add(color(220, 118, 18));
  palette.add(color(189, 50, 0));

  color c = palette.getNorm(0.0);
  for (int i = 0; i < height; i++) {
    stroke(lerpColor(color(32), color(0), (float) i / (float) height));
    line(0, i, width, i);
  }
 
   smooth();
    noStroke();
    frameRate(60);
    background(-1);
    noCursor();
     shapeCenter  = new PVector(width/2, height/2);
    currentVertex= new PVector();
    lookupTable= new PVector[samples];
    for (int i = 0; i < samples; i++)
    {
         float angle= i*angleStep + HALF_PI;
         lookupTable[i] = new PVector(cos(angle), sin(angle));
    }
  minim = new Minim(this);
  players[0]= minim.loadFile("Warriors.mp3");
  players[1]= minim.loadFile("itstime.mp3");
  radiusStep = maxRadius/(players[0].length()*200/players[1].length());
    players[1].loop();
   
}

void draw()
{
  background(0);

   if (players[1].isPlaying())
    {
         visualizeWaves(players[1].right.toArray(), 0x50ff0000, maxRadius, scaleF);
         visualizeWaves(players[1].left.toArray(), 0x50000000, maxRadius, scaleF);
         visualizeWaves(players[1].mix.toArray(), 0x77777777, maxRadius -= radiusStep, scaleF);
    }
}

void visualizeWaves(float [] wavesToVisualize, color fillColor, float radius, float scale_factor)
{
     fill(fillColor);
     stroke(0x10ffffff);
     for (int i = 0; i < wavesToVisualize.length; i++)
     {
        float  r = radius + wavesToVisualize[i]*scale_factor;
          currentVertex = PVector.add(shapeCenter, PVector.mult(lookupTable[i], r));
          vertex (currentVertex.x, currentVertex.y);
              p.update();
    p2.update();
    w.update();
      }
}

No comments:

Post a Comment