User Tools

Site Tools


cs276:cm0project2
//Mitch Taylor
//Final project: Accidental things
//I got really frustrated at how much extracting
//audio data was failing, so I looked for help
//online. Instead, I found a neat program that
//I wanted to emulate, and this is what came
//out of it.

import ddf.minim.*;
import ddf.minim.signals.*;
import ddf.minim.analysis.*;
import ddf.minim.effects.*;

Minim minim;
AudioPlayer pl;
SampleListener smp;
ArrayList sampleSet;
final int displaySamples = 50;

void setup()
{
  //width MUST be a multiple of 256.
  size(1024,800);
  background(0);
  //frameRate set because with an unrestricted
  //framerate, the graphic hurts my head
  frameRate(15);

  minim = new Minim(this);


//pick one, or rename the file and include your own.
  pl = minim.loadFile("classic.mp3", 256);
//  pl = minim.loadFile("bombastic.mp3", 256);
//  pl = minim.loadFile("allthemetal.mp3", 256);


//Microphone input instead of a file. Note: untested.
// in = minim.getLineIn(Minim.MONO, 512);

  smp = new SampleListener();
  
  //sampleSet carries a list of the arrays
  //of sound data sent by Minim
  sampleSet = new ArrayList();
  pl.addListener(smp);
  pl.loop();
}

void draw()
{
  background(0);

  //Took a while to figure this one out. If you
  //don't check this, then the draw method will get
  //ahead of itself and end up causing errors from
  //the arraylist
  if(smp.init)
  {
    sampleSet.add(smp.getSample());
    drawLines(sampleSet);
    
    if (sampleSet.size() > displaySamples)
     sampleSet.remove(0);
  }
}

public void stop()
{
 minim.stop();
 super.stop();
}

void drawLines(ArrayList samples)
{
  float[] snd;

  pushMatrix();
  //for each float array in the arraylist, draw
  //a series of lines representative of the audio
  //data.
  for(int i = samples.size() - 1; i >= 0; i--)
  {
    snd = (float[])samples.get(i);  

    //Determine draw color
    float drawColor = 0;
    for(int j = 0; j < snd.length-1; j++)
      drawColor += snd[j];
    drawColor=abs(drawColor/256);
  
    if(drawColor > 0.15)
      stroke(255,0,0);
    else if(drawColor > 0.06)
      stroke(255,255,0);
    else if(drawColor > 0.02)
      stroke(0,255,255);
    else
      stroke(0,0,255);
    //end determine draw color

    //draw line segments, using an output from
    //the sound data arraylist to determine each
    //point to draw lines between.
    int widthMod = width/256;
    for(int j = 0; j < snd.length-1; j++)
      line(j*widthMod, height + snd[j]*50, (j+1)*widthMod, height + snd[j+1]*50);

    //reposition for repeated loops
    translate(0, -1*height/displaySamples);
  }
  popMatrix();

}


//This is simply a class that does all
//the things inside the AudioListener
//interface without anything extra.
//For more info, check the Minim documentation.
class SampleListener implements AudioListener
{
  private float[] left;
  private float[] right;
  boolean init;
  
  SampleListener()
  {
    left = null; 
    right = null;
  }
  
  synchronized void samples(float[] samp)
  {
    left = samp;
    init = true;
  }
  
  synchronized void samples(float[] sampL, float[] sampR)
  {
    left = sampL;
    right = sampR;
    init = true;
  }
  
  float[] getSample()
  {
    return left;
  }
}
cs276/cm0project2.txt · Last modified: 2012/12/11 09:24 by taylocm0