FX Class: Filter

Interactive example


Overview

Filters are used to alter the harmonic content of a signal. This Filter effect internally is a classic IIR (Infinite Impulse Response) filter. It doesn't allocate any internal buffers and needs just a few bytes of memory.

Filter is an IIR filter based on the typical direct form 1 formula:

y[n] = (b0/a0)*x[n] + (b1/a0)*x[n-1] + (b2/a0)*x[n-2] - (a1/a0)*y[n-1] - (a2/a0)*y[n-2]

Implementation example

The Filter is initialized with an initial filter type and sample rate. It has five adjustable properties: frequency, decibel, resonance, octave, and slope.

Different Filter types are available as constants on the Filter class:

  • Parametric
  • Resonant_Lowpass
  • Resonant_Highpass
  • Bandlimited_Bandpass
  • Bandlimited_Notch
  • LowShelf
  • HighShelf
  • CustomCoefficients

CustomCoefficients allows to use your own IIR filter coefficients. You should call setCustomCoefficients() in the following way:

// Changes will be smoothly handled to prevent audio artifacts. Do not call this concurrently with process().
this.filter.setCustomCoefficients(
1, // b0/a0
1, // b1/a0
1, // b2/a0
1, // a1/a0
1 // a2/a0
);
// Changes will be smoothly handled to prevent audio artifacts. Do not call this concurrently with process().
filter->setCustomCoefficients(
1, // b0/a0
1, // b1/a0
1, // b2/a0
1, // a1/a0
1 // a2/a0
);

The filter is an FX class, it expects both an input and output buffer. The process() function should be called on every loop of the audio processing block.

class SuperpoweredFilterProcessor extends SuperpoweredWebAudio.AudioWorkletProcessor {
// Called when the AudioWorklet is ready to start processing audio
onReady() {
this.filter = new this.Superpowered.Filter(
Superpowered.Filter.Resonant_Lowpass, // The initial filter type
44100 // initial sample rate
);
this.filter.enabled = true;
}
// Runs before the node is destroyed.
// Clean up memory and objects here (such as free allocated linear memory or destruct Superpowered objects).
onDestruct() {
this.filter.destruct();
}
// The shape of the message we pass from our main thread is up to you
onMessageFromMainScope(message) {
if (typeof message.frequency !== 'undefined') this.filter.frequency = message.frequency;
if (typeof message.decibel !== 'undefined') this.filter.decibel = message.decibel;
if (typeof message.resonance !== 'undefined') this.filter.resonance = message.resonance;
if (typeof message.octave !== 'undefined') this.filter.octave = message.octave;
if (typeof message.slope !== 'undefined') this.filter.slope = message.slope;
if (typeof message.type !== 'undefined') this.filter.type = this.Superpowered.Filter[message.type];
if (typeof message.enabled !== 'undefined') this.filter.enabled = Boolean(message.enabled);
}
// This is the process loop which is passed pointers to buffers
// from the WebAudio API (inputBuffer and outputBuffer), with the buffersize (128 samples when running as a native AudioWorklet)
processAudio(inputBuffer, outputBuffer, buffersize) {
// Ensure the samplerate is in sync on every audio processing callback
this.filter.samplerate = this.samplerate;
// Render the output buffers
if (!this.filter.process(inputBuffer.pointer, outputBuffer.pointer, buffersize)) {
for (let n = 0; n < buffersize * 2 ; n++) outputBuffer.array[n] = inputBuffer.array[n];
}
}
}
#include "SuperpoweredFilter.h"
void initFilterEffect() {
unsigned int sampleRate = 44100;
filter = new Superpowered::Filter(Superpowered::Filter::Parametric, sampleRate);
filter->enabled = true;
}
void configureFilterEffect() {
filter->frequency = 4000;
filter->decibel = 6;
}
void processAudio(float *interleavedInput, float *interleavedOutput, unsigned int numberOfFrames, unsigned int samplerate) {
// Ensure the samplerate is in sync on every audio processing callback
filter->samplerate = samplerate;
// Render the output buffers
bool outputChanged = filter->process(interleavedInput, interleavedOutput, numberOfFrames);
...
}

Constants

  • Superpowered.Filter

    CONSTANTS
    Filter types.
    ValueDescription
    Resonant_LowpassResonant lowpass filter - frequency, resonance controllable.
    Resonant_HighpassResonant highpass filter - frequency, resonance controllable.
    Bandlimited_BandpassBandlimited bandpass filter - frequency, octave controllable.
    Bandlimited_NotchBandlimited notch filter - frequency, octave controllable.
    LowShelfLow shelf filter - frequency, slope, decibel controllable.
    HighShelfHigh shelf filter - frequency, slope, decibel controllable.
    ParametricParametric filter - frequency, octave, decibel controllable.
    CustomCoefficientsUser defined filter coefficients. Once set, call setCustomCoefficients().
  • Superpowered::Filter

    CONSTANTS
    Filter types.
    ValueDescription
    Resonant_LowpassResonant lowpass filter - frequency, resonance controllable.
    Resonant_HighpassResonant highpass filter - frequency, resonance controllable.
    Bandlimited_BandpassBandlimited bandpass filter - frequency, octave controllable.
    Bandlimited_NotchBandlimited notch filter - frequency, octave controllable.
    LowShelfLow shelf filter - frequency, slope, decibel controllable.
    HighShelfHigh shelf filter - frequency, slope, decibel controllable.
    ParametricParametric filter - frequency, octave, decibel controllable.
    CustomCoefficientsUser defined filter coefficients. Once set, call setCustomCoefficients().

Properties

decibel

PROPERTY
Decibel gain value for shelving and parametric filters.
TypeMinMaxDefault
Number
-9624

decibel

PROPERTY
Decibel gain value for shelving and parametric filters.
TypeMinMaxDefault
float
-9624

frequency

PROPERTY
Frequency in Hz.
TypeMinMaxDefault
Number
1Half of the samplerate.

frequency

PROPERTY
Frequency in Hz.
TypeMinMaxDefault
float
1Half of the samplerate.

octave

PROPERTY
Width in octave for bandlimited and parametric filters.
TypeMinMaxDefault
Number
0.055

octave

PROPERTY
Width in octave for bandlimited and parametric filters.
TypeMinMaxDefault
float
0.055

resonance

PROPERTY
Resonance value for resonant filters. Resonance = Q / 10.
TypeMinMaxDefault
Number
0.011

resonance

PROPERTY
Resonance value for resonant filters. Resonance = Q / 10.
TypeMinMaxDefault
float
0.011

slope

PROPERTY
Slope value for shelving filters.
TypeMinMaxDefault
Number
0.001 (most gradual slope)1 (steepest slope)

slope

PROPERTY
Slope value for shelving filters.
TypeMinMaxDefault
float
0.001 (most gradual slope)1 (steepest slope)

type

PROPERTY
Filter type. Changing the filter type often involves changing other parameters as well. Therefore in a real-time context change the parameters and the type in the same thread with the process() call.
TypeMinMaxDefault
Number

type

PROPERTY
Filter type. Changing the filter type often involves changing other parameters as well. Therefore in a real-time context change the parameters and the type in the same thread with the process() call.
TypeMinMaxDefault
Superpowered.Filter
Inherited from FX Class

enabled

PROPERTY
Turns the effect on/off. The actual switch will happen on the next process() call for smooth, audio-artifact free operation.
TypeMinMaxDefault
Boolean
false

enabled

PROPERTY
Turns the effect on/off. The actual switch will happen on the next process() call for smooth, audio-artifact free operation.
TypeMinMaxDefault
bool
false

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 the Filter class.
    Parameters
    NameTypeDescription
    typeSuperpowered.Filter.FilterTypeThe initial filter type.
    samplerateNumberThe initial sample rate in Hz.
    Returns
    TypeDescription
    Superpowered.FilterThe constructed instance.
  • constructor

    METHOD
    Creates an instance of the Filter class.
    Parameters
    NameTypeDefaultDescription
    typeSuperpowered::Filter::FilterTypeThe initial filter type.
    samplerateunsigned intThe initial sample rate in Hz.
    Returns
    TypeDescription
    Superpowered::FilterThe constructed instance.
  • processMono

    METHOD
    Processes/renders mono audio. Always call it in the audio processing callback, regardless if the effect is enabled or not for smooth, audio-artifact free operation. It's never blocking for real-time usage. You can change all effect properties on any thread, concurrently with process().
    Parameters
    NameTypeDescription
    inputNumber (pointer in Linear Memory)32-bit mono input.
    outputNumber (pointer in Linear Memory)32-bit mono output.
    numberOfFramesNumberNumber of frames to process. Recommendations for best performance: multiply of 4, minimum 64.
    Returns
    TypeDescription
    BooleanIf process() returns with true, the contents of output are replaced with the audio output. If process() returns with false, it indicates silence. The contents of output are not changed in this case (not overwritten with zeros).
  • processMono

    METHOD
    Processes/renders mono audio. Always call it in the audio processing callback, regardless if the effect is enabled or not for smooth, audio-artifact free operation. It's never blocking for real-time usage. You can change all effect properties on any thread, concurrently with process().
    Parameters
    NameTypeDefaultDescription
    inputfloat *32-bit mono input.
    outputfloat *32-bit mono output.
    numberOfFramesunsigned intNumber of frames to process. Recommendations for best performance: multiply of 4, minimum 64.
    Returns
    TypeDescription
    boolIf process() returns with true, the contents of output are replaced with the audio output. If process() returns with false, it indicates silence. The contents of output are not changed in this case (not overwritten with zeros).
  • setCustomCoefficients

    METHOD
    For advanced use. Set custom coefficients for the filter. Changes will be smoothly handled to prevent audio artifacts. Do not call this concurrently with process().
    Parameters
    NameTypeDescription
    b0/a0NumberFirst feed forward/backward coefficient.
    b1/a0NumberSecond feed forward/backward coefficient.
    b2/a0NumberThird feed forward/backward coefficient.
    a1/a0NumberForth feed forward/backward coefficient.
    a2/a0NumberFifth feed forward/backward coefficient.
    Returns
    None
  • setCustomCoefficients

    METHOD
    For advanced use. Set custom coefficients for the filter. Changes will be smoothly handled to prevent audio artifacts. Do not call this concurrently with process().
    Parameters
    NameTypeDefaultDescription
    b0/a0floatFirst feed forward/backward coefficient.
    b1/a0floatSecond feed forward/backward coefficient.
    b2/a0floatThird feed forward/backward coefficient.
    a1/a0floatForth feed forward/backward coefficient.
    a2/a0floatFifth feed forward/backward coefficient.
    Returns
    None
Inherited from FX Class
  • destruct

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

    METHOD
    Processes/renders the audio. Always call it in the audio processing callback, regardless if the effect is enabled or not for smooth, audio-artifact free operation. It's never blocking for real-time usage.
    Parameters
    NameTypeDescription
    inputNumber (pointer in Linear Memory)32-bit interleaved stereo input.
    outputNumber (pointer in Linear Memory)32-bit interleaved stereo output.
    numberOfFramesNumberNumber of frames to process. Recommendations for best performance: multiply of 4, minimum 64.
    Returns
    TypeDescription
    BooleanIf process() returns with true, the contents of output are replaced with the audio output. If process() returns with false, it indicates silence. The contents of output are not changed in this case (not overwritten with zeros).
  • process

    METHOD
    Processes/renders the audio. Always call it in the audio processing callback, regardless if the effect is enabled or not for smooth, audio-artifact free operation. It's never blocking for real-time usage. You can change all effect properties on any thread, concurrently with process().
    Parameters
    NameTypeDefaultDescription
    inputfloat *32-bit interleaved stereo input.
    outputfloat *32-bit interleaved stereo output.
    numberOfFramesunsigned intNumber of frames to process. Recommendations for best performance: multiply of 4, minimum 64.
    Returns
    TypeDescription
    boolIf process() returns with true, the contents of output are replaced with the audio output. If process() returns with false, it indicates silence. The contents of output are not changed in this case (not overwritten with zeros).

v1.0.33