VOCAL DRAWING

////////////////////////////////////////////////////////////////////////
////TAB 1////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
////Code to draw with sound, applying specific drawing for each frequency//// 
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////



void setup() {
//size of the canvas  
  size(600, 600, P3D);
//color of background  
  background(0);
//element for the sound analysis  
  minim = new Minim(this);
  in = minim.getLineIn();
  fft = new FFT( in.bufferSize(), in.sampleRate() );
//smooth the image  
  smooth();
}


void draw() {
  analysis(); 
  visual();
  interaction();
}

// function to draw element trigger by certain frequency
void interaction() {
  if (fft.getBand(/* band number x */) > /* seuil */) {
//Draw something
  }
}

// function to save image by pressing key 's'
void record() {
  if (keyPressed == true) {
    if (key == 'S' || key == 's') {
      int i = 1;
      save("img"+i+".png");
      println("IMAGE SAVED");
      i++;
      delay(10);
    }
  }
}


////////////////////////////////////////////////////////////////////////
////TAB 2///////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
//part of the code that analyse frequencies of the sound from the mic///
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////

//add library minim
import ddf.minim.analysis.*;
import ddf.minim.*;

//declare object
Minim       minim;
AudioInput in;
FFT         fft;

//number of separate part of frequencies
int nBands = 64;

//function to analyse sound 
void analysis() {
  fft.forward( in.mix ); 
}

//function to create a visual feedback to see which frequency is trigger by sound
void visual() { 
  background(0);
  stroke(0);
  fill(255);  
  for(int i = 0; i < fft.specSize(); i++) {
     if (fft.getBand(i) < 15) {
       fill(255);
     }
     else {
       fill(255, 0, 0);
     }    
    rect(i*(width/nBands), height, (width/nBands), -fft.getBand(i)*4);
  }
}

// function to get info value from the band where you clicked with the mouse 
void mousePressed() {
  float bands = map(mouseX, 0, width, 0, nBands);
  int band = int(bands);
  float val = fft.getBand(band);
  
  println("band:" +band + " / value: "+val);
  
}