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

int comb_width = 1;

AudioThread audioThread;
// we'll use this to store the audio data we read from the audio file
float[] audioData;
// we'll use this to remember our position in the audio data array
int readHead;

FFT fft;
//SineWave sine;
float[] buffer;
float[] out_buff;
int bsize = 4096;

void setup() {
  size(512, 500);
  // the audio file we want to play, which should be in the data dir
  String audioFilename = "amenloop.wav";
  // read the audio data into a float array using the AudioFileIn class
  audioData = new AudioFileIn(audioFilename, this).getSampleData();
  // print how many samples we read
  println("Read "+audioData.length+" samples");
  // set the read head to zero, the first sample
  readHead = 0;
  // start up the audio thread
  audioThread = new AudioThread();
  // fft code
  fft = new FFT(bsize, 44100);
  //fft.window(FFT.HAMMING);
  buffer = new float[bsize];
  // fill the buffer with a sine wave
  //sine.generate(buffer);

  audioThread.start();
}

void draw() {
  background(0);
  stroke(255);
  for (int i = 0; i < fft.specSize(); i++)
  {
    // draw the line for frequency band i, scaling it by 4 so we can see it a bit better
    line(i, height, i, height - fft.getBand(i)*4);
  }
}

// this function gets called when you press the escape key in the sketch
void stop() {
  // tell the audio to stop
  audioThread.quit();
  // call the version of stop defined in our parent class, in case it does anything vital
  super.stop();
}


// this gets called by the audio thread when it wants some audio
// we should fill the sent buffer with the audio we want to send to the 
// audio output
void generateAudioOut(float[] output) {


  for (int i=0;i<output.length; i++) {
    // copy data from the audio we read from the file (audioData)
    // into the buffer that gets sent to the sound card
    output[i] =audioData[readHead];
    readHead = (readHead + 1) % audioData.length;

    //readHead = (readHead + 1) % audioData.length;
  }
  // now do forward fft on the buffer
  fft.forward(output);
  // mess with the spectrum
  //for (int i=mouseX*2;i<fft.specSize(); i++){
  //  fft.setBand(i,0);
  // }
  threshold(fft, (float) comb_width / 10);
  comb(fft, comb_width);
  comb_width ++;
  //comb_width = comb_width + 1;
  // inverse it back into the buffer
  fft.inverse(output);
}



void threshold(FFT fft, float threshold) {  
  for (int i=0; i < fft.specSize(); i++) {
    // filtering out quiet components
    if (fft.getBand(i) < threshold) {
      fft.setBand(i, 0);
    }
  }
}

void comb(FFT fft, int comb_width) {
  for (int i=0; i < fft.specSize(); i+=(comb_width * 2)) {
    // filtering out quiet components
    for (int j=0;j<comb_width && i+j < fft.specSize(); j++, i++) {
      fft.setBand(i, 0);
    }
  }
}


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

AudioThread audioThread;
// we'll use this to store the audio data we read from the audio file
float[] audioData;
// we'll use this to remember our position in the audio data array
int readHead;

FFT fft;
//SineWave sine;
float[] buffer;
float[] out_buff;
int bsize = 4096;

void setup() {
  size(512, 500);
  // the audio file we want to play, which should be in the data dir
  String audioFilename = "myk_hats_dub1.wav";
  // read the audio data into a float array using the AudioFileIn class
  audioData = new AudioFileIn(audioFilename, this).getSampleData();
  // print how many samples we read
  println("Read "+audioData.length+" samples");
  // set the read head to zero, the first sample
  readHead = 0;
  // start up the audio thread
  audioThread = new AudioThread();
  // fft code
  fft = new FFT(bsize, 44100);
  //fft.window(FFT.HAMMING);
  buffer = new float[bsize];
  // fill the buffer with a sine wave
  //sine.generate(buffer);

  audioThread.start();
}

void draw() {
  background(0);
  stroke(255);
  for (int i = 0; i < fft.specSize(); i++)
  {
    // draw the line for frequency band i, scaling it by 4 so we can see it a bit better
    line(i, height, i, height - fft.getBand(i)*4);
  }
}

// this function gets called when you press the escape key in the sketch
void stop() {
  // tell the audio to stop
  audioThread.quit();
  // call the version of stop defined in our parent class, in case it does anything vital
  super.stop();
}


// this gets called by the audio thread when it wants some audio
// we should fill the sent buffer with the audio we want to send to the 
// audio output
void generateAudioOut(float[] output) {


  for (int i=0;i<output.length; i++) {
    // copy data from the audio we read from the file (audioData)
    // into the buffer that gets sent to the sound card
    output[i] =audioData[readHead];
    readHead = (readHead + 1) % audioData.length;

    //readHead = (readHead + 1) % audioData.length;
  }
  // now do forward fft on the buffer
  fft.forward(output);
  // mess with the spectrum
  //for (int i=mouseX*2;i<fft.specSize(); i++){
  //  fft.setBand(i,0);
 // }
  for (int i=0; i < fft.specSize(); i++){
    //println(fft.getBand(i));
    if (fft.getBand(i) < mouseY / 10){
      fft.setBand(i, 0);
    }  
    //i+=100;
    
  }
  
  
  // inverse it back into the buffer
  fft.inverse(output);
}