Audio Visualisation

If you look at the output from your println, you will see that you are getting very small values. You need to adjust your amplitude from 15 to 1500 so to be able to have a responsive sketch. Now, you will need to visualize the frequency of your incoming data before you start making decisions about what data to plot, what to select and what to discard. You will need to study the fft of a single sound sample, become familiar with it, and then extract the information from it before you start testing across different sound sources.

Kf


//REFERENCE: https://forum.processing.org/two/discussion/23097/how-can-i-determine-which-spectrum-is-generated-from-which-file-on-mouseclick#latest



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

Minim minim;
AudioInput in;
FFT fft;
AudioPlayer ap;
// how many individual peak bands we have (dep. binsperband)
float gain = 0; // in dB
float dB_scale = 2.0;  // pixels per dB
int buffer_size = 1024;  // also sets FFT size (frequency resolution)
float sample_rate = 44100;

int spectrum_height = 200; // determines range of dB shown
int legend_height = 20;
int spectrum_width = 512; // determines how much of spectrum we see
int legend_width = 40;
int freqOffset = 0;

final color colSelected=color(255,0,0);
final color colPlain=color(255);

void setup()
{
  size(552, 220, P2D);
  textMode(SCREEN);
  //textFont(createFont("Georgia", 12));

  minim = new Minim(this);

  //in = minim.getLineIn(Minim.MONO,buffer_size,sample_rate);
  ap = minim.loadFile("da.mp3");
  // create an FFT object that has a time-domain buffer 
  // the same size as line-in's sample buffer
  fft = new FFT(ap.bufferSize(), ap.sampleRate());

  // Tapered window important for log-domain display
  fft.window(FFT.HAMMING);

  //println("buffer:"+in.bufferSize());
  //println("sampleRate:"+in.sampleRate());
  //println("peaksize:"+peaksize);
}


void draw()
{
  // clear window
  background(0);

  // perform a forward FFT on the samples in input buffer
  fft.forward(ap.mix);
  ap.play();

  // now draw current spectrum in brighter blue
  stroke(64, 255, 255);
  noFill();
  for (int i = 0; i < fft.specSize(); i++) {

    // draw the line for frequency band i using dB scale
    float val = dB_scale*(20*((float)Math.log10(fft.getBand(i))) + gain);

    if (fft.getBand(i) == 0) {   
      val = -200;
    }  // avoid log(0)

    int y = spectrum_height - Math.round(val);

    if (y > spectrum_height) { 
      y = spectrum_height;
    }

    float x1=legend_width+i+freqOffset;
    float y1=spectrum_height;
    float x2=x1;
    float y2=y;

    line(x1, y1, x2, y2);

    pushStyle();
    if (dist(mouseX, mouseY, x2, y2)<20) 
      fill(colSelected);
    else
      fill(colPlain);
    noStroke();
    ellipse(x2, y2, 3, 3);
    popStyle();

    // update the peak record
    // which peak bin are we in?
    //int peaksi = i/binsperband;
    //if (val > peaks[peaksi]) {
    //  peaks[peaksi] = val;
    //  // reset peak age counter
    //  peak_age[peaksi] = 0;
    //}
  }
  // add legend
  // frequency axis
  fill(255);

  stroke(255);

  int y = spectrum_height;

  line(legend_width, y, legend_width+spectrum_width, y); // horizontal line

  // x,y address of text is immediately to the left of the middle of the letters 
  textAlign(CENTER, TOP);

  int spFreq=0; //for spacing

  for (float freq = 0.0; freq < ap.sampleRate(); freq += 2000.0) {
    int x = legend_width+spFreq+freqOffset; // which bin holds this frequency
    //println(freq+"->"+fft.freqToIndex(freq));
    line(x, y, x, y+4); // tick mark
    text(Math.round(freq/1000) +"kHz", x, y+5); // add text label
    spFreq+=45;
  }

  // DBlevel axis
  int x = legend_width;

  line(x, 0, x, spectrum_height); // vertictal line

  textAlign(RIGHT, CENTER);

  for (float level = -100.0; level < 100.0; level += 20.0) {
    y = spectrum_height - (int)(dB_scale * (level+gain));

    line(x, y, x-3, y);

    text((int)level+" dB", x-5, y);
  }
}

void keyReleased()
{
  // +/- used to adjust gain on the fly
  if (key == '+' || key == '=') {
    gain = gain + 5.0;
  } else if (key == '-' || key == '_') {
    gain = gain - 5.0;
  }
  //(.)/(/) used to adjust frequency axis
  else if (key == '/')
  {
    freqOffset = freqOffset-4;
  } else if ( key == '.')
  {
    freqOffset = freqOffset+4;
  }
}

void stop()
{
  // always close Minim audio classes when you finish with them
  in.close();
  minim.stop();

  super.stop();
}