Prototyping – Fourth & Final Iteration

This iteration addresses what I feel are the last of my issues with my audio visualizer, and I now have a clean, fully functional version of it.

import ddf.minim.*;

Minim minim;
AudioPlayer player;

int col1 = 100;
int col2 = 100;
int x ;
int y ;

void setup()
{
  frameRate(30);
  size(1280, 620, P3D);
  background(0) ;
  smooth ();
  float x = 6;
  minim = new Minim(this);  
  player = minim.loadFile("time.mp3");
  player.loop();
}

void draw()
{
  translate(0, 310);
  
  for (int i = 0; i < player.bufferSize () - 1; i++) {
    
   if (col1 < 255) {
     col1 = 100+int(((1+(player.right.get(i+1)))/2)*155) ;
    } else {
    col1 = 0;
  }
  
   if (col2 < 255) {
     col2 = 100+int(((1+(player.left.get(i+1)))/2)*155) ;
    } else {
      col2 = 0;
  }
  
  // 
  int y1 = int((player.left.get(i+1))*305);
  int y2 = int((player.right.get(i+1))*305);
  
  // turn get values into actual amplitude by preventing negative values
  if (y1 < 0) {
    y1 = -y1;
  }
  
  if (y2 <0) {
    y2 = -y2;
  }
    
    if (i == 0) {
      
      strokeWeight(5);
      
      //top line
      stroke(col1,0,0);
      line(x,0,x,0-y1-10);
      
      //bottom line
      stroke(0,0,col2);
      line(x,0,x,10+y2);
      
      //seperator
      strokeWeight(2);
      stroke(0);
      line(0,0,1280,0);
    
    }
  }
  
  //Fade lines into background
  fill(0,15);
  noStroke();
  rect(0,-310,width,height);
  
  if (x < width) {
    x = x + 6;
  } else {
    x = 6;
  }
  
}

void mousePressed (){
  background (0);
}

This iteration has only one notable change from the previous version, the addition of lines 45-52. Using “if” conditions allowed me to change any negative values of y1 or y2 to their positive counterparts. While this means that -0.5 would return the same value as 0.5, this is reflective of the nature of amplitude – the peak of a wave will be extremely similar in value, if not identical, to its trough, so by making positive and negative values identical the visualizer now displays the actual amplitude of the audio input.

Final iteration.
Final iteration.

Due to this change I also removed the addition of 1 to the y1 and y2 integers, as now all values will be on a scale of 0 to 1 anyway. The result is a visualizer that scales accurately with the volume and intensity of the audio track’s input, and while it may not truly represent the player.left/right.get values, the audio and visual elements appear more synchronized from an audience’s point of view.

Now that the visualizer is fully functional, I have prepared my final and testing-ready prototype, by changing the form of audio input from Minim’s audio player to its audio input (to take sound from a microphone). The final code, to be used in my testing, is shown below:

import ddf.minim.*;

Minim minim;
AudioInput in;

int col1 = 100;
int col2 = 100;
int x ;
int y ;

void setup()
{
  frameRate(30);
  size(1280, 620, P3D);
  background(0) ;
  smooth ();
  float x = 6;
  minim = new Minim(this);
  in = minim.getLineIn();
}

void draw()
{
  translate(0, 310);
  
  for (int i = 0; i < in.bufferSize() -1; i++) {
        
   if (col1 < 255) {
     col1 = 75+int(in.right.get(i)*255) ;
    } else {
    col1 = 0;
  }
  
   if (col2 < 255) {
     col2 = 75+int(in.left.get(i)*255) ;
    } else {
      col2 = 0;
  }
  
  int y1 = int(in.left.get(i)*305);
  int y2 = int(in.right.get(i)*305);
    
  if (i == 0) {
      
      strokeWeight(5);
      
      //top line
      stroke(col1,75,75);
      line(x,0,x,0-y1);
      
      //bottom line
      stroke(col2,75,75);
      line(x,0,x,y2);
      
      //seperator
      strokeWeight(2);
      stroke(0);
      line(0,0,1280,0);
    
    }
  }
  
  //Fade lines into background
  fill(0,15);
  noStroke();
  rect(0,-310,width,height);
  
  if (x < width) {
    x = x + 6;
  } else {
    x = 6;
  }
  
}

void mousePressed (){
  background (0);
}

More detailed pictures and videos of my project in the space will be taken in the testing phase to show how the visualizer actually reacts to audio input – creating a video output through Processing so far has proven to create frame rate issues as .tiff files are created.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s