Tuesday, 30 September 2014

Simple sketch

I wanted the visualization to have more of a shape, so I tried drawing a circle connecting the wave values with the radius and vertexes around it...? I include the basic tempo code thing from minim library to compare the two visualizations created.

It is still rough and not very interesting yet (I think). Doesn't really create a sort of unique feel of the song as I wanted. However I like how the shape changes dramatically when the song picks up its pace.


Code:

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

Minim minim;
AudioPlayer player;
int
  samples = 1024;
float
  angleStep  = TWO_PI/samples,
  maxRadius = 300.,
  scaleF = 125.,
  radiusStep;
PVector
  shapeCenter,
  currentVertex;
PVector[]
  lookupTable;
 
 
void setup()
{
  size(1000, 1000, P3D);
   smooth();
    noStroke();
    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);
  player = minim.loadFile("Warriors.mp3");
  radiusStep = maxRadius/(player.length()*6/100);
    player.loop();
   
}

void draw()
{
  background(0);
  stroke(255);
   for(int i = 0; i < player.bufferSize() - 1; i++)
  {
    float x1 = map( i, 0, player.bufferSize(), 0, width );
    float x2 = map( i+1, 0, player.bufferSize(), 0, width );
    line( x1, 50 + player.left.get(i)*50, x2, 50 + player.left.get(i+1)*50 );
    line( x1, 150 + player.right.get(i)*50, x2, 150 + player.right.get(i+1)*50 );
  }
 
   if (player.isPlaying())
    {
         visualizeWaves(player.right.toArray(), 0x50ff0000, maxRadius, scaleF);
         visualizeWaves(player.left.toArray(), 0x50000000, maxRadius, scaleF);
         visualizeWaves(player.mix.toArray(), 0xffffffff, maxRadius -= radiusStep, scaleF);
    }
}

void visualizeWaves(float [] wavesToVisualize, color fillColor, float radius, float scale_factor)
{
     fill(fillColor);
     stroke(0x10ffffff);
     beginShape();
     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);
      }
      endShape();
}

No comments:

Post a Comment