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:
this.filter.setCustomCoefficients(
1,
1,
1,
1,
1
);
filter->setCustomCoefficients(
1,
1,
1,
1,
1
);
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 {
onReady() {
this.filter = new this.Superpowered.Filter(
Superpowered.Filter.Resonant_Lowpass,
44100
);
this.filter.enabled = true;
}
onDestruct() {
this.filter.destruct();
}
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);
}
processAudio(inputBuffer, outputBuffer, buffersize) {
this.filter.samplerate = this.samplerate;
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) {
filter->samplerate = samplerate;
bool outputChanged = filter->process(interleavedInput, interleavedOutput, numberOfFrames);
...
}
Constants
Superpowered.Filter
CONSTANTSFilter types.
Value | Description |
---|
Resonant_Lowpass | Resonant lowpass filter - frequency, resonance controllable. |
Resonant_Highpass | Resonant highpass filter - frequency, resonance controllable. |
Bandlimited_Bandpass | Bandlimited bandpass filter - frequency, octave controllable. |
Bandlimited_Notch | Bandlimited notch filter - frequency, octave controllable. |
LowShelf | Low shelf filter - frequency, slope, decibel controllable. |
HighShelf | High shelf filter - frequency, slope, decibel controllable. |
Parametric | Parametric filter - frequency, octave, decibel controllable. |
CustomCoefficients | User defined filter coefficients. Once set, call setCustomCoefficients(). |
Superpowered::Filter
CONSTANTSFilter types.
Value | Description |
---|
Resonant_Lowpass | Resonant lowpass filter - frequency, resonance controllable. |
Resonant_Highpass | Resonant highpass filter - frequency, resonance controllable. |
Bandlimited_Bandpass | Bandlimited bandpass filter - frequency, octave controllable. |
Bandlimited_Notch | Bandlimited notch filter - frequency, octave controllable. |
LowShelf | Low shelf filter - frequency, slope, decibel controllable. |
HighShelf | High shelf filter - frequency, slope, decibel controllable. |
Parametric | Parametric filter - frequency, octave, decibel controllable. |
CustomCoefficients | User defined filter coefficients. Once set, call setCustomCoefficients(). |
Properties
decibel
PROPERTYDecibel gain value for shelving and parametric filters.
Type | Min | Max | Default |
---|
Number | -96 | 24 | |
decibel
PROPERTYDecibel gain value for shelving and parametric filters.
Type | Min | Max | Default |
---|
float | -96 | 24 | |
frequency
PROPERTYFrequency in Hz.
Type | Min | Max | Default |
---|
Number | 1 | Half of the samplerate. | |
frequency
PROPERTYFrequency in Hz.
Type | Min | Max | Default |
---|
float | 1 | Half of the samplerate. | |
octave
PROPERTYWidth in octave for bandlimited and parametric filters.
Type | Min | Max | Default |
---|
Number | 0.05 | 5 | |
octave
PROPERTYWidth in octave for bandlimited and parametric filters.
Type | Min | Max | Default |
---|
float | 0.05 | 5 | |
resonance
PROPERTYResonance value for resonant filters. Resonance = Q / 10.
Type | Min | Max | Default |
---|
Number | 0.01 | 1 | |
resonance
PROPERTYResonance value for resonant filters. Resonance = Q / 10.
Type | Min | Max | Default |
---|
float | 0.01 | 1 | |
slope
PROPERTYSlope value for shelving filters.
Type | Min | Max | Default |
---|
Number | 0.001 (most gradual slope) | 1 (steepest slope) | |
slope
PROPERTYSlope value for shelving filters.
Type | Min | Max | Default |
---|
float | 0.001 (most gradual slope) | 1 (steepest slope) | |
type
PROPERTYFilter 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.
type
PROPERTYFilter 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.
Type | Min | Max | Default |
---|
Superpowered.Filter | | | |
Inherited from FX Classenabled
PROPERTYTurns the effect on/off. The actual switch will happen on the next process() call for smooth, audio-artifact free operation.
Type | Min | Max | Default |
---|
Boolean | | | false |
enabled
PROPERTYTurns the effect on/off. The actual switch will happen on the next process() call for smooth, audio-artifact free operation.
Type | Min | Max | Default |
---|
bool | | | false |
samplerate
PROPERTYThe sample rate of the audio context, you must update if it changes.
samplerate
PROPERTYThe sample rate of the audio context, you must update if it changes.
Type | Min | Max | Default |
---|
unsigned int | | | |
Methods
constructor
METHODCreates an instance of the Filter class.
ParametersName | Type | Description |
---|
type | Superpowered.Filter.FilterType | The initial filter type. |
samplerate | Number | The initial sample rate in Hz. |
ReturnsType | Description |
---|
Superpowered.Filter | The constructed instance. |
constructor
METHODCreates an instance of the Filter class.
ParametersName | Type | Default | Description |
---|
type | Superpowered::Filter::FilterType | | The initial filter type. |
samplerate | unsigned int | | The initial sample rate in Hz. |
ReturnsType | Description |
---|
Superpowered::Filter | The constructed instance. |
processMono
METHODProcesses/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().
ParametersName | Type | Description |
---|
input | Number (pointer in Linear Memory) | 32-bit mono input. |
output | Number (pointer in Linear Memory) | 32-bit mono output. |
numberOfFrames | Number | Number of frames to process. Recommendations for best performance: multiply of 4, minimum 64. |
ReturnsType | Description |
---|
Boolean | If 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
METHODProcesses/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().
ParametersName | Type | Default | Description |
---|
input | float * | | 32-bit mono input. |
output | float * | | 32-bit mono output. |
numberOfFrames | unsigned int | | Number of frames to process. Recommendations for best performance: multiply of 4, minimum 64. |
ReturnsType | Description |
---|
bool | If 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
METHODFor advanced use. Set custom coefficients for the filter. Changes will be smoothly handled to prevent audio artifacts. Do not call this concurrently with process().
ParametersName | Type | Description |
---|
b0/a0 | Number | First feed forward/backward coefficient. |
b1/a0 | Number | Second feed forward/backward coefficient. |
b2/a0 | Number | Third feed forward/backward coefficient. |
a1/a0 | Number | Forth feed forward/backward coefficient. |
a2/a0 | Number | Fifth feed forward/backward coefficient. |
ReturnsNone
setCustomCoefficients
METHODFor advanced use. Set custom coefficients for the filter. Changes will be smoothly handled to prevent audio artifacts. Do not call this concurrently with process().
ParametersName | Type | Default | Description |
---|
b0/a0 | float | | First feed forward/backward coefficient. |
b1/a0 | float | | Second feed forward/backward coefficient. |
b2/a0 | float | | Third feed forward/backward coefficient. |
a1/a0 | float | | Forth feed forward/backward coefficient. |
a2/a0 | float | | Fifth feed forward/backward coefficient. |
ReturnsNone
Inherited from FX Classdestruct
METHODDestructor to free Linear Memory.
ParametersNone
ReturnsNone
process
METHODProcesses/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.
ParametersName | Type | Description |
---|
input | Number (pointer in Linear Memory) | 32-bit interleaved stereo input. |
output | Number (pointer in Linear Memory) | 32-bit interleaved stereo output. |
numberOfFrames | Number | Number of frames to process. Recommendations for best performance: multiply of 4, minimum 64. |
ReturnsType | Description |
---|
Boolean | If 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
METHODProcesses/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().
ParametersName | Type | Default | Description |
---|
input | float * | | 32-bit interleaved stereo input. |
output | float * | | 32-bit interleaved stereo output. |
numberOfFrames | unsigned int | | Number of frames to process. Recommendations for best performance: multiply of 4, minimum 64. |
ReturnsType | Description |
---|
bool | If 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). |