topical media & game development

talk show tell print

graphic-processing-site-examples-Libraries-Minim--Sound--ForwardFFT-ForwardFFT.pde / pde



  
Forward FFT by Damien Di Fede. This sketch demonstrates how to use an FFT to analyze an AudioBuffer and draw the resulting spectrum. It also allows you to turn windowing on and off, but you will see there is not much difference in the spectrum. Press 'w' to turn on windowing, press 'e' to turn it off.

  
  
  import ddf.minim.analysis.*;
  import ddf.minim.*;
  
  Minim minim;
  AudioPlayer jingle;
  FFT fft;
  String windowName;
  
  void setup()
  {
    size(512, 200, P3D);
    textMode(SCREEN);
    minim = new Minim(this);
    
    jingle = minim.loadFile("jingle.mp3", 2048);
    jingle.loop();
    // create an FFT object that has a time-domain buffer the same size as jingle's sample buffer
    // note that this needs to be a power of two and that it means the size of the spectrum
    // will be 512. see the online tutorial for more info.
    fft = new FFT(jingle.bufferSize(), jingle.sampleRate());
    textFont(createFont("Arial", 16));
    windowName = "None";
  }
  
  void draw()
  {
    background(0);
    stroke(255);
    // perform a forward FFT on the samples in jingle's left buffer
    // note that if jingle were a MONO file, this would be the same as using jingle.right or jingle.left
    fft.forward(jingle.mix);
    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);
    }
    fill(255);
    // keep us informed about the window being used
    text("The window being used is: " + windowName, 5, 20);
  }
  
  void keyReleased()
  {
    if ( key == 'w' ) 
    {
      // a Hamming window can be used to shape the sample buffer that is passed to the FFT
      // this can reduce the amount of noise in the spectrum
      fft.window(FFT.HAMMING);
      windowName = "Hamming";
    }
    
    if ( key == 'e' ) 
    {
      fft.window(FFT.NONE);
      windowName = "None";
    }
  }
  
  void stop()
  {
    // always close Minim audio classes when you finish with them
    jingle.close();
    minim.stop();
    
    super.stop();
  }
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.