OpenFrameworks II

May 29 2017

OpenFrameworks FFT


Following on from the first experiment in OpenFrameworks a visualization using FFT and SoundPlayer.  FFT is not difficult to utilize in relation to ofSoundPlayer class - the magnitude of frequency bands are returned in the ofSoundGetSpectrum () "global function".  If one wishes to implement FFTs in relation to live audio input or using ofSoundStream class it can be done - however it is not so painless. 


As a starting point for the visual component I made use of the same class "Flow" that was used in the envelope test.  This class allowed the construction of various objects each allotted a separate "bin" from the sound spectrum.  The advantage of using FFT's over some kind of peak amplitude analysis is the control. The different objects on the picture plane can be controlled through frequency ranges.  This is not very fine grained in the present example.  There are ways to make transforms more sophisticated in connection to the actual "notes" that are appearing in a .wav or an audio input.  In the present case however the coarse-grained approach was the one taken up.


Some of the code utilized in the work will be produced below.  There is nothing too complicated there however.  The difficult thing is to work out which ways to scale the different objects (in relation to size, color etc.) with the incoming spectrum information.  Of course most of the information that is utilizable (insofar as it connects to frequency bins that one is actually hearing) is in the first series of bins.   

void ofApp::draw(){

    ofBackground(0);

    

    float *val = ofSoundGetSpectrum( N );

   

    for (int i = flows.size()-1; i >=0; i--) {

        

        float rf=flows[i].spectrum[i+1] *((i*20)*2);

        float rg=flows[i].spectrum[i+1] *((i*5)*50);

        float rb=flows[i].spectrum[i+1] *((i+50)+(i*10));

        float ra=flows[i].spectrum[i+1] *((i+50)+(i*2));

        ofSetColor((int)rf%255,(int)rg%255,(int)rb%255, (int)ra%255);

        //printf("%d \n",(int)rf);

        flows[i].draw();

        for ( int j=0; j<N; j++ ) {

            flows[i].spectrum[j] *= 0.97; 

            flows[i].spectrum[j] = max( flows[i].spectrum[j], val[j] );

  

            flows[i].twistY = ofMap( flows[i].spectrum[ flows[i].bandRadz[i] ], 1, 3, ofRandom(100),ofRandom(400), true );

            flows[i].countX = ofMap(flows[i]. spectrum[ flows[i].bandVel ], 0, 1, 1, 250 );

        }

        

    }

    

}

This code represents only the draw function in the ofApp.cpp file.  The body of the FFT stuff is simply applying the FFT function from SoundPlayer class in oF.  Shown above is a way to distribute the FFT frequency bins to a series of objects - a different effect or color for each object.  These effects are rhythmic but not in the basic RMS peak form, rather in a more spectrum oriented form.  NB:  Some of the code here is custom work and some comes from the oF documentation.  The "flows" class is built around 2 functions taken from "Creative coding de-mystified" (D. Prevalov).  The functions are turned into a class which is then re-scaled, organized into vectors and triggered on various levels by FFT bins (color and size are manipulated).

A screen recording shows the different effects that one can organize in this manner.