ofxFft

repository·master·Indexed 40 days ago

https://github.com/kylemcdonald/ofxfft

An openFrameworks addon providing Fast-Fourier Transform (FFT) capabilities. It serves as a wrapper for the KISS FFT and FFTW libraries and includes the ProcessFFT class for high-level audio frequency analysis, frequency band extraction (SuperLow, Low, Mid, High), and spectral visualization.

Tokens
1.8K
Snippets
8
Records
11
Agent score
0%

What's inside ofxFft

  1. Install ofxFft

    master

    To install ofxFft, move the ofxFft folder into your of/addons/ directory.

    If you are using openFrameworks version 0.9.0 or later, the OF Project Generator will automatically add the required compiler search paths if you configure the project to include ofxFft.

    mv ofxFft of/addons/
  2. Switch from KISS FFT to FFTW

    master

    By default, ofxFft uses KISS FFT (included with OF). To use FFTW instead, follow these steps:

    1. Download the precompiled libraries.
    2. Define OFX_FFT_USE_FFTW in your compiler flags/preprocessor settings.
    3. If on Windows, add the FFTW include path and link the specific DLL.
    4. Update your code to use the FFTW flag in the ofxFft::create call.
  3. Configure project files for ofxFft examples

    master

    The project files for the included examples (like example-basic or example-eq) are not included in the repository. You must generate them using the openFrameworks ProjectGenerator:

    1. Point the ProjectGenerator to addons/ofxFft.
    2. Change the project name to the exact name of the example you want to generate (e.g., example-basic).
    3. Ensure ofxFft is selected from the addons list.
    4. Click "Generate".
  4. Use FFTW in ofxFft code

    master

    To explicitly use the FFTW backend in your code (e.g., in inApp.cpp), modify the ofxFft::create call to include the OF_FFT_FFTW flag.

    // Standard KISS FFT usage:
    // fft = ofxFft::create(bufferSize, OF_FFT_WINDOW_HAMMING);
    
    // FFTW usage:
    fft = ofxFft::create(bufferSize, OF_FFT_WINDOW_HAMMING, OF_FFT_FFTW);
  5. FFTW Windows-specific setup

    master

    If you are using FFTW on Windows, you must also perform these additional steps:

    1. Add the include directory: of/addons/ofxFft/libs/fftw/include
    2. Link with the library: of/addons/ofxFft/libs/fftw/lib/win32/libfftw3f-3.dll
    Include path: of/addons/ofxFft/libs/fftw/include
    Link with: of/addons/ofxFft/libs/fftw/lib/win32/libfftw3f-3.dll
  6. Enable FFTW via compiler flags

    master

    To enable FFTW support, you must add the OFX_FFT_USE_FFTW definition to your project settings based on your development environment:

    ### Xcode
    Select Project -> Build Settings -> Other C++ Flags
    Add: `-DOFX_FFT_USE_FFTW`
    
    ### Linux (Makefiles & Codeblocks)
    Edit `config.make` in your project folder and add to the `USER_CFLAGS` line:
    `-DOFX_FFT_USE_FFTW`
    
    ### Codeblocks (Windows)
    Right-click project -> "Build options..." -> Select project name (not release/debug) -> "Compiler settings" tab -> "#defines" tab -> Add: `OFX_FFT_USE_FFTW`
    
    ### Visual Studio
    Right-click project -> Properties -> Set Configuration to "All Configurations" -> Configuration Properties -> C/C++ -> Preprocessor -> Preprocessor Definitions -> Add: `OFX_FFT_USE_FFTW`
  7. Retrieve frequency band and audio metrics from ProcessFFT

    master

    The ProcessFFT class provides several methods to extract processed audio data:

    Frequency Bands:

    • getSuperLowVal()
    • getLowVal()
    • getMidVal()
    • getHighVal()
    • getLoudBand(): Returns the index of the loudest frequency band for the current frame.

    Audio Metrics:

    • getLoudBand(): Returns the loudness of the loudest band.
    • getUnscaledLoudestValue(): Returns the raw loudest value.
    • getSmoothedUnscaledLoudestValue(): Returns a smoothed version of the raw loudest value.
    • getDelta(): Returns the change in loudness.
    • getIntensityAtFrequency(float _freq): Returns the intensity at a specific frequency.
    • getSpectrum(): Returns a vector<float> containing the full spectrum amplitudes.
    • getFFTpercentage(): Returns the current percentage of the FFT being used.
  8. Use the ProcessFFT class for audio frequency analysis

    master

    The ProcessFFT class provides a high-level interface for performing Fast Fourier Transforms (FFT) on audio data within an openFrameworks project. It simplifies frequency analysis by providing pre-calculated values for specific frequency bands (SuperLow, Low, Mid, High) and various audio metrics like loudness and spectral intensity.

    To use it, you typically call setup() during your application's initialization and update() within your main update loop to process the incoming audio buffer.

    ProcessFFT fftProcessor;
    
    // In setup()
    fftProcessor.setup();
    
    // In update()
    fftProcessor.update();
    
    // In draw()
    float lowVal = fftProcessor.getLowVal();
  9. Configure ProcessFFT parameters

    master

    You can tune how ProcessFFT analyzes audio using several setter methods:

    • setNumFFTBins(int _numFFTBins): Sets the number of frequency columns being analyzed (e.g., 16, 32).
    • setFFTpercentage(float _FFTpercentage): Determines how much of the FFT spectrum is parsed into bins (e.g., 0.2 for 20% of the 0-5kHz range).
    • setExponent(float _Exponent): A factor (typically 1.0 to 1.6) used to weight high-end frequencies more heavily to compensate for high-energy bass.
    • setNormalize(bool _normalize): If true, values are returned in a 0.0 to 1.0 range. If false, values use the range defined by setVolumeRange.
    • setVolumeRange(int _volumeRange): Sets the upper bound for values when normalization is disabled.
    • setHistorySize(int _framesOfHistory): Sets the number of frames to keep for history/averaging.
    fftProcessor.setNumFFTBins(32);
    fftProcessor.setFFTpercentage(0.2f);
    fftProcessor.setExponent(1.2f);
    fftProcessor.setNormalize(true);
  10. Visualize ProcessFFT data

    master

    For debugging and visualization, ProcessFFT includes built-in drawing methods:

    • drawBars(): Renders visual bars representing the FFT.
    • drawDebug(): Renders debug information.
    • drawHistoryGraph(ofPoint pt, fftRangeType drawType): Draws a history graph at the specified position for a specific frequency range. Valid fftRangeType values are:
      • SUPERLOW
      • LOW
      • MID
      • HIGH
      • MAXSOUND
    fftProcessor.drawBars();
    fftProcessor.drawHistoryGraph(ofPoint(100, 100), LOW);