Class: BandpassFilterbank

Overview

Efficient bandpass filter bank for real-time zero latency frequency analysis. Each band is a separated bandpass filter with custom width and center frequency.


How to implement

// Constructor.
let frequencies = new Superpowered.Float32Buffer(8), widths = new Superpowered.Float32Buffer(8);
for (let n = 0; n < 8; n++) {
frequencies.array[n] = n * 1000;
widths.array[n] = 1;
}
let filterbank = new Superpowered.BandpassFilterbank(
8, // The number of bands. Must be a multiply of 8.
frequencies.pointer, // Center frequencies of each band in Hz.
widths.pointer, // Widths of each band in octave (1.0 is one octave, 1.0 / 12.0 is one halfnote).
44100, // The initial sample rate in Hz.
0 // numGroups: for advanced use.
// The filter bank can be set up with multiple frequency + width groups, then process() or processNoAdd() can be performed with one specific frequency + width group. For example, set up one group with wide frequency coverage for the 20-20000 Hz range and three additional groups for 20-200 Hz, 200-2000 Hz and 2000-20000 Hz. When processing with the wide group of 20-20000 Hz and the highest magnitude can be found at 1000 Hz, use the 200-2000 Hz group for the next process() or processNoAdd() call, so the filter bank will have a "focus" on a narrower range.
// If numGroups > 0, then the number of frequencies and widths should be numGroups * numBands. Example: for numBands = 8 and numGroups = 2, provide 8 + 8 frequencies and 8 + 8 widths.
);
Superpowered.free(frequencies);
Superpowered.free(widths);
// Do this when the sample rate changes.
filterbank.samplerate = 48000;
// Processes the audio. Has no return value.
// It will ADD to the current magnitude in bands (like bands[0] += 0.123), so you can "measure" the magnitude of each frequency for a longer period of time.
// To calculate a result between 0 and 1 for multiple consecutive process() calls, divide each value in bands with the total number of frames passed to the consecutive process() calls.
filterbank.process(
input, // Input pointer (audio in 32-bit floating point numbers, stereo, interleaved).
128, // Number of frames to process.
0 // The group index for advanced "grouped" usage.
);
// Processes the audio. Has no return value.
filterbank.processNoAdd(
input, // Input pointer (audio in 32-bit floating point numbers, stereo, interleaved).
128, // Number of frames to process.
0 // The group index for advanced "grouped" usage.
);
// The magnitude of the frequency bands. Will be updated after each process() or processNoAdd() call.
let bands = filterbank.getBands(); // Superpowered.Float32Buffer
let band0_magnitude = bands.array[0];
// Sets all values of bands to 0. Has no return value.
filterbank.resetBands();
// Returns with the average volume of all audio passed to all previous process() or processNoAdd() calls.
let averageVolume = filterbank.getAverageVolume();
// Returns with the cumulated absolute value of all audio passed to all previous process() or processNoAdd() calls. Like you would add the absolute value of all audio samples together.
let sumAudio = filterbank.getSumVolume();
// Resets the sum and average volume value to start measurement anew. Has no return value.
filterbank.resetSumAndAverageVolume();
// Returns with the peak volume of all audio passed to all previous process() or processNoAdd() calls.
let peakVolume = filterbank.getPeakVolume();
// Resets the peak volume value to start measurement anew. Has no return value.
filterbank.resetPeakVolume();
// Destructor (to free up memory).
filterbank.destruct();
#include "SuperpoweredBandpassFilterbank.h"
// Constructor.
float frequencies[8] = {};
float widths[8] = {};
for (int n = 0; n < 8; n++) {
frequencies[n] = n * 1000;
widths[n] = 1;
}
Superpowered::BandpassFilterbank *filterbank = new Superpowered::BandpassFilterbank(
8, // The number of bands. Must be a multiply of 8.
frequencies, // Center frequencies of each band in Hz.
widths, // Widths of each band in octave (1.0 is one octave, 1.0 / 12.0 is one halfnote).
44100, // The initial sample rate in Hz.
0 // numGroups: for advanced use.
// The filter bank can be set up with multiple frequency + width groups, then process() or processNoAdd() can be performed with one specific frequency + width group. For example, set up one group with wide frequency coverage for the 20-20000 Hz range and three additional groups for 20-200 Hz, 200-2000 Hz and 2000-20000 Hz. When processing with the wide group of 20-20000 Hz and the highest magnitude can be found at 1000 Hz, use the 200-2000 Hz group for the next process() or processNoAdd() call, so the filter bank will have a "focus" on a narrower range.
// If numGroups > 0, then the number of frequencies and widths should be numGroups * numBands. Example: for numBands = 8 and numGroups = 2, provide 8 + 8 frequencies and 8 + 8 widths.
);
// Do this when the sample rate changes.
filterbank->samplerate = 48000;
// Processes the audio. Has no return value.
// It will ADD to the current magnitude in bands (like bands[0] += 0.123), so you can "measure" the magnitude of each frequency for a longer period of time.
// To calculate a result between 0 and 1 for multiple consecutive process() calls, divide each value in bands with the total number of frames passed to the consecutive process() calls.
filterbank->process(
input, // Input pointer (audio in 32-bit floating point numbers, stereo, interleaved).
128, // Number of frames to process.
0 // The group index for advanced "grouped" usage.
);
// Processes the audio. Has no return value.
filterbank->processNoAdd(
input, // Input pointer (audio in 32-bit floating point numbers, stereo, interleaved).
128, // Number of frames to process.
0 // The group index for advanced "grouped" usage.
);
// The magnitude of the frequency bands. Will be updated after each process() or processNoAdd() call.
float *bands = filterbank->getBands();
float band0_magnitude = bands[0];
// Sets all values of bands to 0. Has no return value.
filterbank->resetBands();
// Returns with the average volume of all audio passed to all previous process() or processNoAdd() calls.
float averageVolume = filterbank->getAverageVolume();
// Returns with the cumulated absolute value of all audio passed to all previous process() or processNoAdd() calls. Like you would add the absolute value of all audio samples together.
float sumAudio = filterbank->getSumVolume();
// Resets the sum and average volume value to start measurement anew. Has no return value.
filterbank->resetSumAndAverageVolume();
// Returns with the peak volume of all audio passed to all previous process() or processNoAdd() calls.
float peakVolume = filterbank->getPeakVolume();
// Resets the peak volume value to start measurement anew. Has no return value.
filterbank->resetPeakVolume();

Properties

samplerate

PROPERTY
The sample rate of the audio context, you must update if it changes.
TypeMinMaxDefault
Number

samplerate

PROPERTY
The sample rate of the audio context, you must update if it changes.
TypeMinMaxDefault
unsigned int

Methods

  • constructor

    METHOD
    Creates an instance of BandpassFilterbank class.
    Parameters
    NameTypeDescription
    numBandsNumberThe number of bands. Must be a multiply of 8.
    frequenciesNumber (pointer in Linear Memory)Center frequencies of each band in Hz.
    widthsNumber (pointer in Linear Memory)Widths of each band in octave (1.0f is one octave, 1.0f / 12.0f is one halfnote).
    samplerateNumberThe initial sample rate in Hz.
    numGroupsNumber

    For advanced use. The filter bank can be set up with multiple frequency + width groups, then process() or processNoAdd() can be performed with one specific frequency + width group. For example, set up one group with wide frequency coverage for the 20-20000 Hz range and three additional groups for 20-200 Hz, 200-2000 Hz and 2000-20000 Hz.

    When processing with the wide group of 20-20000 Hz and the highest magnitude can be found at 1000 Hz, use the 200-2000 Hz group for the next process() or processNoAdd() call, so the filter bank will have a "focus" on a narrower range. If numGroups > 0, then the number of frequencies and widths should be numGroups * numBands. Example: for numBands = 8 and numGroups = 2, provide 8 + 8 frequencies and 8 + 8 widths.

    Returns
    TypeDescription
    Superpowered.BandpassFilterbankThe constructed instance
  • constructor

    METHOD
    Creates an instance of BandpassFilterbank class.
    Parameters
    NameTypeDefaultDescription
    numBandsunsigned intThe number of bands. Must be a multiply of 8.
    frequenciesfloat *Center frequencies of each band in Hz.
    widthsfloat *Widths of each band in octave (1.0f is one octave, 1.0f / 12.0f is one halfnote).
    samplerateunsigned intThe initial sample rate in Hz.
    numGroupsunsigned int0

    For advanced use. The filter bank can be set up with multiple frequency + width groups, then process() or processNoAdd() can be performed with one specific frequency + width group. For example, set up one group with wide frequency coverage for the 20-20000 Hz range and three additional groups for 20-200 Hz, 200-2000 Hz and 2000-20000 Hz.

    When processing with the wide group of 20-20000 Hz and the highest magnitude can be found at 1000 Hz, use the 200-2000 Hz group for the next process() or processNoAdd() call, so the filter bank will have a "focus" on a narrower range. If numGroups > 0, then the number of frequencies and widths should be numGroups * numBands. Example: for numBands = 8 and numGroups = 2, provide 8 + 8 frequencies and 8 + 8 widths.

    Returns
    TypeDescription
    Superpowered::BandpassFilterbankThe constructed instance
  • destruct

    METHOD
    Destructor to free Linear Memory.
    Parameters
    None
    Returns
    None
  • getAverageVolume

    METHOD
    Returns with the average volume of all audio passed to all previous process() or processNoAdd() calls.
    Parameters
    None
    Returns
    TypeDescription
    NumberAverage volume
  • getAverageVolume

    METHOD
    Returns with the average volume of all audio passed to all previous process() or processNoAdd() calls.
    Parameters
    None
    Returns
    TypeDescription
    floatAverage volume
  • getBands

    METHOD
    The magnitude of each frequency band. Updated after each process() or processNoAdd() call.
    Parameters
    None
    Returns
    TypeDescription
    Superpowered.Float32BufferFloating point numbers representing the magnitude of each frequency band.
  • getBands

    METHOD
    The magnitude of each frequency band. Updated after each process() or processNoAdd() call.
    Parameters
    None
    Returns
    TypeDescription
    float *Floating point numbers representing the magnitude of each frequency band.
  • getPeakVolume

    METHOD
    Returns with the peak volume of all audio passed to all previous process() or processNoAdd() calls.
    Parameters
    None
    Returns
    TypeDescription
    NumberAumulated absolute value
  • getPeakVolume

    METHOD
    Returns with the peak volume of all audio passed to all previous process() or processNoAdd() calls.
    Parameters
    None
    Returns
    TypeDescription
    floatAumulated absolute value
  • getSumVolume

    METHOD
    Returns with the cumulated absolute value of all audio passed to all previous process() or processNoAdd() calls. Like you would add the absolute value of all audio samples together.
    Parameters
    None
    Returns
    TypeDescription
    NumberAumulated absolute value
  • getSumVolume

    METHOD
    Returns with the cumulated absolute value of all audio passed to all previous process() or processNoAdd() calls. Like you would add the absolute value of all audio samples together.
    Parameters
    None
    Returns
    TypeDescription
    floatAumulated absolute value
  • process

    METHOD
    Processes the audio. It will ADD to the current magnitude in bands (like bands[0] += 0.123), so you can "measure" the magnitude of each frequency for a longer period of time. To calculate a result between 0 and 1 for multiple consecutive process() calls, divide each value in bands with the total number of frames passed to the consecutive process() calls.
    Parameters
    NameTypeDescription
    inputNumber (pointer in Linear Memory)32-bit interleaved stereo input. Special case: set to NULL to empty all buffered content.
    numberOfFramesNumberNumber of frames to input and output.
    groupNumberThe group index for advanced "grouped" usage.
    Returns
    None
  • process

    METHOD
    Processes the audio. It will ADD to the current magnitude in bands (like bands[0] += 0.123), so you can "measure" the magnitude of each frequency for a longer period of time. To calculate a result between 0 and 1 for multiple consecutive process() calls, divide each value in bands with the total number of frames passed to the consecutive process() calls.
    Parameters
    NameTypeDefaultDescription
    inputfloat *32-bit interleaved stereo input. Special case: set to NULL to empty all buffered content.
    numberOfFramesunsigned intNumber of frames to input and output.
    groupint0The group index for advanced "grouped" usage.
    Returns
    None
  • processNoAdd

    METHOD
    Processes the audio. It will replace the contents of bands.
    Parameters
    NameTypeDescription
    inputNumber (pointer in Linear Memory)32-bit interleaved stereo input. Special case: set to NULL to empty all buffered content.
    numberOfFramesNumberNumber of frames to input and output.
    groupNumberThe group index for advanced "grouped" usage.
    Returns
    None
  • processNoAdd

    METHOD
    Processes the audio. It will replace the contents of bands.
    Parameters
    NameTypeDefaultDescription
    inputfloat *32-bit interleaved stereo input. Special case: set to NULL to empty all buffered content.
    numberOfFramesunsigned intNumber of frames to input and output.
    groupint0The group index for advanced "grouped" usage.
    Returns
    None
  • resetBands

    METHOD
    Sets all values of bands to 0.
    Parameters
    None
    Returns
    None
  • resetBands

    METHOD
    Sets all values of bands to 0.
    Parameters
    None
    Returns
    None
  • resetPeakVolume

    METHOD
    Resets the peak volume value to start measurement anew.
    Parameters
    None
    Returns
    None
  • resetPeakVolume

    METHOD
    Resets the peak volume value to start measurement anew.
    Parameters
    None
    Returns
    None
  • resetSumAndAverageVolume

    METHOD
    Resets the sum and average volume value to start measurement anew.
    Parameters
    None
    Returns
    None
  • resetSumAndAverageVolume

    METHOD
    Resets the sum and average volume value to start measurement anew.
    Parameters
    None
    Returns
    None

v1.0.6