
import ddf.minim.analysis.*;
import ddf.minim.*;
import ddf.minim.effects.*;
PImage bg;
PImage bg2;


Minim minim = new Minim(this);
AudioPlayer in;
FFT fft;
int w;
PImage fade;
int hVal;
float rWidth, rHeight;

int MAX_LINES = 20;
int MAX_TONES = 10;

boolean bRect;
float barWidth;
String[] songs = new String[4];

int trackNum=1;
boolean bOnce=false;


void setup() {

  songs[0]="01 Nature Of Inviting.mp3";
  songs[1]="02 Satellite.mp3";
  songs[2]="12 Electric Chapel.mp3";
  songs[3]="01 Meltdown (feat. Lorde, Pusha T, Q.mp3";

  size (1024, 768); //P3D
  bg = loadImage("water.jpg");
  bg2 = loadImage("water3.jpg");
  in = minim.loadFile(songs[0], 1024);
  in.play();
  //in.loop();
  fft = new FFT(in.bufferSize(), in.sampleRate());
  fft.logAverages(22, 2);

  barWidth = width/fft.avgSize();

  stroke(255);
  w = width/40;
  strokeWeight(w);
  strokeCap(SQUARE);
  background(0);
  fade = get(0, 0, width, height); //save current screen image


  rWidth= width * 0.99;
  rHeight = height * 0.99;
  hVal=0;

  println("To skip through the song: press 'S'");
  println("To pause the song: press 'P'");
  println("To resume the song: press 'R'");
  println("To choose song 1: press '1'");
  println("To choose song 2: press '2'");
  println("To choose song 3: press '3'");
  println("To choose song 4: press '4'");
}

void draw() {

  noFill();
  noStroke();
  rectMode(CORNERS);
  if (trackNum==1)
  {
  background(bg);
  }
  else if (trackNum==2)
  {
    background(bg2);
  }

  tint(255, 255, 255, 252);
  //image(fade, (width-rWidth)*2, (height-rHeight)*2, rWidth, rHeight);
  image(fade, (width-rWidth)/2, (height-rHeight)*4, rWidth, rHeight);

  noTint();


  frame.setTitle(floor(frameRate) + " fps // " + "T-" + (in.length() - in.position())); 

  // orward FFT on the samples in active's mix buffer
  fft.forward(in.mix);

  colorMode(HSB);
  stroke(hVal, 255, 255);
  colorMode(RGB);

//IAMX- Nature of Inviting
  if (trackNum==1)
  {
   
    stroke(random(0, mouseX), random(mouseY, 0), random(255));
    strokeWeight(3);
    for (int i=0; i<in.bufferSize ()-1; i++) {
      float x1 = map(i, 0, in.bufferSize(), 0, width);
      float x2 = map(i+1, 0, in.bufferSize(), 0, width);
      line(x1, 50 + in.right.get(i)*50, x2, 150 + in.right.get(i+1)*50);
      //line(x1, 50 + in.left.get(i)*50, x2, 50 + in.left.get(i+1)*50);
      //line(x1, 250 + in.right.get(i)*50, x2, 150 + in.right.get(i+1)*50);
      //line(x1, 350 + in.right.get(i)*50, x2, 150 + in.right.get(i+1)*50);
      //stroke(map(i,0,player.bufferSize()-1,0,360),90,90);
    }
    // display the FFT logarithmic averages as a standard equalizer -_-
    stroke(0);
    strokeWeight(3);
    for (int i=0; i<fft.avgSize (); i++) {
      // draw a rectangle for each average
      fill(map(i, 0, fft.avgSize()-1, 0, 360), 100, 100);
      //rect(i*barWidth, height, i*barWidth + barWidth, height - fft.getAvg(i));
    }


    fade = get(0, 0, width, height);


//    for (int i=0; i<fft.avgSize (); i++) {
//      // draw a rectangle for each average 
//
//      fill(map(i, 0, fft.avgSize()-1, 0, 360), 100, 100);
//      rect(i*barWidth, height, i*barWidth + barWidth, height - fft.getAvg(i));
//      //rect(i*barWidth, height, i*barWidth + barWidth, height - fft.getAvg(i));
//    }
  }

//The Kills- Satellite
  if (trackNum==2)
  {
    //background(118,149,198);

    // draw the left & right channel waveforms, the values returned by mix.get()
    // will be between -1 and 1, so we need to scale them up to see the waveform
    stroke(255);
    strokeWeight(3);
    for (int i=0; i<in.bufferSize ()-1; i++) {
      float x1 = map(i, 0, in.bufferSize(), 0, width);
      float x2 = map(i+1, 0, in.bufferSize(), 0, width);
      line(x1, 50 + in.left.get(i)*50, x2, 50 + in.left.get(i+1)*50);
      line(x1, 150 + in.right.get(i)*50, x2, 150 + in.right.get(i+1)*50);
      //stroke(map(i,0,player.bufferSize()-1,0,360),90,90);
    }

    fade = get(0, 0, width, height);


    //    for (int i=0; i<fft.avgSize(); i++) {
    //      // draw a rectangle for each average
    //      fill(map(i, 0, fft.avgSize()-1, 0, 360), 100, 100);
    //      rect(i*barWidth, height, i*barWidth + barWidth, height - fft.getAvg(i));
    //    }
  }
  
//Lady Gaga- Electric Chapel
  if (trackNum==3)
  {

    if (bOnce==false)
    {
      bOnce=true;
      barWidth = width/fft.avgSize();
      stroke(255);
      w = width/40;
      strokeWeight(w);
      strokeCap(SQUARE);
      background(0);
      fade = get(0, 0, width, height); //save current screen image


      rWidth= width * 0.99;
      rHeight = height * 0.99;
      hVal=0;
    }

    noFill();
    noStroke();
    rectMode(CORNERS);
    background(0);

    tint(255, 255, 255, 252);
    //image(fade, (width-rWidth)*2, (height-rHeight)*2, rWidth, rHeight);
    image(fade, (width-rWidth)/2, (height-rHeight)/2, rWidth, rHeight);

    noTint();

    frame.setTitle(floor(frameRate) + " fps // " + "T-" + (in.length() - in.position())); 

    // perform a forward FFT on the samples in active's mix buffer
    fft.forward(in.mix);

    colorMode(HSB);
    stroke(hVal, 255, 255);
    colorMode(RGB);

    //background(0);
    stroke(255);
    strokeWeight(3);

    for (int i=0; i<in.bufferSize ()-1; i++) {
      float x1 = map(i, 0, in.bufferSize(), 0, width);
      float x2 = map(i+1, 0, in.bufferSize(), 0, width);
      line(x1, 50 + in.left.get(i)*50, x2, 50 + in.left.get(i+1)*50);
      //line(x1, 150 + in.right.get(i)*50, x2, 150 + in.right.get(i+1)*50);
      //line(x1, 250 + in.right.get(i)*50, x2, 250 + in.right.get(i+1)*50);
      stroke(map(i,0,in.bufferSize()-1,0,360),90,90);
    }


    // display the FFT logarithmic averages as a standard equalizer -_-
    stroke(0);
    strokeWeight(2);
    for (int i=0; i<fft.avgSize (); i++) {
      // draw a rectangle for each average
      fill(map(i, 0, fft.avgSize()-1, 0, 360), 100, 100);
      rect(i*barWidth, height, i*barWidth + barWidth, height - fft.getAvg(i));
    }


    fade = get(0, 0, width, height);


    for (int i=0; i<fft.avgSize (); i++) {
      // draw a rectangle for each average
      fill(map(i, 0, fft.avgSize()-1, 0, 360), 100, 100);
      rect(i*barWidth, height, i*barWidth + barWidth, height - fft.getAvg(i));
    }
  }
  
  //Lorde, Pusha- Meltdown
  if (trackNum==4)
  {

    if (bOnce==false)
    {
      bOnce=true;
      barWidth = width/fft.avgSize();
      stroke(255);
      w = width/40;
      strokeWeight(w);
      strokeCap(SQUARE);
      background(0);
      fade = get(0, 0, width, height); //save current screen image


      rWidth= width * 0.99;
      rHeight = height * 0.99;
      hVal=0;
    }

    noFill();
    noStroke();
    rectMode(CORNERS);
    background(0);

    tint(255, 255, 255, 252);
    //image(fade, (width-rWidth)*2, (height-rHeight)*2, rWidth, rHeight);
    image(fade, (width-rWidth)/2, (height-rHeight)/2, rWidth, rHeight);

    noTint();

    frame.setTitle(floor(frameRate) + " fps // " + "T-" + (in.length() - in.position())); 

    // perform a forward FFT on the samples in active's mix buffer
    fft.forward(in.mix);

    colorMode(HSB);
    stroke(hVal, 255, 255);
    colorMode(RGB);

    //background(0);
    stroke(255);
    strokeWeight(3);

    for (int i=0; i<in.bufferSize ()-1; i++) {
      float x1 = map(i, 0, in.bufferSize(), 0, width);
      float x2 = map(i+1, 0, in.bufferSize(), 0, width);
      line(x1, 50 + in.left.get(i)*50, x2, 50 + in.left.get(i+1)*50);
      line(x1, 150 + in.right.get(i)*50, x2, 150 + in.right.get(i+1)*50);
      line(x1, 250 + in.right.get(i)*50, x2, 250 + in.right.get(i+1)*50);
      line(x1, 350 + in.right.get(i)*50, x2, 350 + in.right.get(i+1)*50);
      line(x1, 450 + in.right.get(i)*50, x2, 450 + in.right.get(i+1)*50);
      line(x1, 550 + in.right.get(i)*50, x2, 550 + in.right.get(i+1)*50);
      line(x1, 650 + in.right.get(i)*50, x2, 650 + in.right.get(i+1)*50);
      //line(x1, 750 + in.right.get(i)*50, x2, 750 + in.right.get(i+1)*50);
      stroke(map(i,0,in.bufferSize()-1,0,360),90,90);
    }


//    // display the FFT logarithmic averages as a standard equalizer -_-
//    stroke(0);
//    strokeWeight(2);
//    for (int i=0; i<fft.avgSize (); i++) {
//      // draw a rectangle for each average
//      fill(map(i, 0, fft.avgSize()-1, 0, 360), 100, 100);
//      rect(i*barWidth, height, i*barWidth + barWidth, height - fft.getAvg(i));
//    }
//
//
   fade = get(0, 0, width, height);


//    for (int i=0; i<fft.avgSize (); i++) {
//      // draw a rectangle for each average
//      fill(map(i, 0, fft.avgSize()-1, 0, 360), 100, 100);
//      rect(i*barWidth, height, i*barWidth + barWidth, height - fft.getAvg(i));
//    }
  }
}

void keyPressed() {
  if (key == ENTER)
  {    
    bRect=true;
    in.play();
  }

  if (key == 'p')
    in.pause();

  if (key == 'r')
    in.play();

  if (key == 's')
    in.skip(5000);

  //if (key == 'l')
  //in.loop(); 

  if (key=='1')
  {
    trackNum=1;
    in.close();
    in = minim.loadFile(songs[0], 1024);
    in.play();
    bOnce=false;
  } else if (key=='2')
  {
    trackNum=2;
    in.close();
    in = minim.loadFile(songs[1], 1024);
    in.play();
    bOnce=false;
  } else if (key=='3')
  {
    trackNum=3;
    in.close();
    in = minim.loadFile(songs[2], 1024);
    in.play();
    bOnce=false;
  }  else if (key=='4')
  {
    trackNum=4;
    in.close();
    in = minim.loadFile(songs[3], 1024);
    in.play();
  }
}


void stop()
{
  // always close Minim audio classes when you are done with them
  in.close();
  minim.stop();
  super.stop();
}

