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.Float32Bufferlet 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
PROPERTYType | Min | Max | Default | Number |
---|
samplerate
PROPERTYType | Min | Max | Default | unsigned int |
---|
Methods
constructor
METHODCreates an instance of BandpassFilterbank class.ParametersReturnsName Type Description numBands Number
The number of bands. Must be a multiply of 8. frequencies Number (pointer in Linear Memory)
Center frequencies of each band in Hz. widths Number (pointer in Linear Memory)
Widths of each band in octave (1.0f is one octave, 1.0f / 12.0f is one halfnote). samplerate Number
The initial sample rate in Hz. numGroups Number
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.
Type Description Superpowered.BandpassFilterbank
The constructed instance constructor
METHODCreates an instance of BandpassFilterbank class.ParametersReturnsName Type Default Description numBands unsigned int
The number of bands. Must be a multiply of 8. frequencies float *
Center frequencies of each band in Hz. widths float *
Widths of each band in octave (1.0f is one octave, 1.0f / 12.0f is one halfnote). samplerate unsigned int
The initial sample rate in Hz. numGroups unsigned int
0 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.
Type Description Superpowered::BandpassFilterbank
The constructed instance destruct
METHODDestructor to free Linear Memory.ParametersNoneReturnsNonegetAverageVolume
METHODReturns with the average volume of all audio passed to all previous process() or processNoAdd() calls.ParametersNoneReturnsType Description Number
Average volume getAverageVolume
METHODReturns with the average volume of all audio passed to all previous process() or processNoAdd() calls.ParametersNoneReturnsType Description float
Average volume getBands
METHODThe magnitude of each frequency band. Updated after each process() or processNoAdd() call.ParametersNoneReturnsType Description Superpowered.Float32Buffer
Floating point numbers representing the magnitude of each frequency band. getBands
METHODThe magnitude of each frequency band. Updated after each process() or processNoAdd() call.ParametersNoneReturnsType Description float *
Floating point numbers representing the magnitude of each frequency band. getPeakVolume
METHODReturns with the peak volume of all audio passed to all previous process() or processNoAdd() calls.ParametersNoneReturnsType Description Number
Aumulated absolute value getPeakVolume
METHODReturns with the peak volume of all audio passed to all previous process() or processNoAdd() calls.ParametersNoneReturnsType Description float
Aumulated absolute value getSumVolume
METHODReturns 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.ParametersNoneReturnsType Description Number
Aumulated absolute value getSumVolume
METHODReturns 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.ParametersNoneReturnsType Description float
Aumulated absolute value process
METHODProcesses 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.ParametersReturnsName Type Description input Number (pointer in Linear Memory)
32-bit interleaved stereo input. Special case: set to NULL to empty all buffered content. numberOfFrames Number
Number of frames to input and output. group Number
The group index for advanced "grouped" usage. Noneprocess
METHODProcesses 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.ParametersReturnsName Type Default Description input float *
32-bit interleaved stereo input. Special case: set to NULL to empty all buffered content. numberOfFrames unsigned int
Number of frames to input and output. group int
0 The group index for advanced "grouped" usage. NoneprocessNoAdd
METHODProcesses the audio. It will replace the contents of bands.ParametersReturnsName Type Description input Number (pointer in Linear Memory)
32-bit interleaved stereo input. Special case: set to NULL to empty all buffered content. numberOfFrames Number
Number of frames to input and output. group Number
The group index for advanced "grouped" usage. NoneprocessNoAdd
METHODProcesses the audio. It will replace the contents of bands.ParametersReturnsName Type Default Description input float *
32-bit interleaved stereo input. Special case: set to NULL to empty all buffered content. numberOfFrames unsigned int
Number of frames to input and output. group int
0 The group index for advanced "grouped" usage. NoneresetBands
METHODSets all values of bands to 0.ParametersNoneReturnsNoneresetBands
METHODSets all values of bands to 0.ParametersNoneReturnsNoneresetPeakVolume
METHODResets the peak volume value to start measurement anew.ParametersNoneReturnsNoneresetPeakVolume
METHODResets the peak volume value to start measurement anew.ParametersNoneReturnsNoneresetSumAndAverageVolume
METHODResets the sum and average volume value to start measurement anew.ParametersNoneReturnsNoneresetSumAndAverageVolume
METHODResets the sum and average volume value to start measurement anew.ParametersNoneReturnsNone