Quantcast
Channel: Library Questions - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 2896

How to get an octave based frequency spectrum from the FFT in Minim

$
0
0

I want to create audio visuals like the bars visual that is commonly used in music videos. Minim has an FFT that gets the amplitude of each frequency band, but each band is evenly spaced. So the result is that the bars are mostly showing only the high frequencies, and the mid and low frequencies are bunched up on the left. I tried this, but I'm not good with logarithms and have no clue what the correct math to calculate this should be, and it didn't really work:

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

Minim minim;
AudioPlayer song;
FFT fft;

// the number of lines/bands to draw
int bands = 512;

float a = (log(20000 - 20) / log(bands));

void setup()
{
  size(512, 200);

  minim = new Minim(this);

  song = minim.loadFile("test.mp3", 2048);
  song.loop();

  fft = new FFT(song.bufferSize(), song.sampleRate());
}

void draw()
{
  background(0);
  stroke(255);

  fft.forward(song.mix);

  for(int i = 0; i < bands; i++)
  {
    // calculate the frequency for the current band on a logamorithmic scale
    float freq = pow(i, a) + 20;

    // get the amplitude at that frequency
    float amplitude = fft.getFreq(freq);

    // convert the amplitude to a DB value.
    // this means values will range roughly from 0 for the loudest
    // bands to some negative value.
    float bandDB = 20 * log(2 * amplitude / fft.timeSize());
    // so then we want to map our DB value to the height of the window
    // given some reasonable range
    float bandHeight = map( bandDB, 0, -150, 0, height );

    // draw the band as a line
    line(i, height, i, bandHeight);
  }
}

I don't even know if it can be done using the FFT in Minim because there might not be enough bands calculated for the lower frequencies. If not, what can I use to accomplish this?


Viewing all articles
Browse latest Browse all 2896

Trending Articles