FX Class: Compressor
Interactive example
Overview
This compressor effect is used to reduce the span between the softest and loudest sounds or dynamic range. It is often used to control maximum levels and maintain higher average loudness.
The inputGainDb
and outputGainDb
properties are set with values between -24 and 24 with a default of 0. The wet
property controls the balance between the audio and the effect with a range of 0 to 1. The attackSec
and releaseSec
properties are limited to values between `0.0001 - 1` and `0.1 - 4` respectively (both in seconds). The thresholdDb
and ratio
properties are integral to the effect with the ratio specifying the amount of attenuation applied to the signal and threshold setting the level at which the compression effect is engaged. The hpCutOffHz
represents the integrated highpass filter frequency which in this case is limited to values between 1 and 1000. Finally, there is a getGainReductionDb()
method which will return the maximum gain reduction in decibels since the last getGainReductionDb()
call.
The compressor class doesn't allocate any internal buffers and needs less than 1 kb of memory.
Implementation example
The compressor is is an FX class, it expects both an input and output buffer. The process()
method should be called on every loop of the audio processing block.
class SuperpoweredCompressorStageProcessor extends SuperpoweredWebAudio.AudioWorkletProcessor {
onReady() {
this.compressor = new this.Superpowered.Compressor(
this.samplerate,
);
this.compressor.enabled = true;
}
onDestruct() {
this.compressor.destruct();
}
onMessageFromMainScope(message) {
if (typeof message.inputGainDb !== 'undefined') this.compressor.inputGainDb = message.inputGainDb;
if (typeof message.outputGainDb !== 'undefined') this.compressor.outputGainDb = message.outputGainDb;
if (typeof message.wet !== 'undefined') this.compressor.wet = message.wet;
if (typeof message.attackSec !== 'undefined') this.compressor.attackSec = message.attackSec;
if (typeof message.releaseSec !== 'undefined') this.compressor.releaseSec = message.releaseSec;
if (typeof message.ratio !== 'undefined') this.compressor.ratio = message.ratio;
if (typeof message.thresholdDb !== 'undefined') this.compressor.thresholdDb = message.thresholdDb;
if (typeof message.hpCutOffHz !== 'undefined') this.compressor.hpCutOffHz = message.hpCutOffHz;
if (typeof message.enabled !== 'undefined') this.compressor.enabled = message.enabled;
}
processAudio(inputBuffer, outputBuffer, buffersize, parameters) {
this.compressor.samplerate = this.samplerate;
if (!this.compressor.process(inputBuffer.pointer, outputBuffer.pointer, buffersize)) {
for (let n = 0; n < buffersize * 2 ; n++) outputBuffer.array[n] = inputBuffer.array[n];
}
}
}
#include "SuperpoweredCompressor.h"
void initCompressor() {
unsigned int sampleRate = 44100;
compressor = new Superpowered::Compressor(sampleRate);
}
void configureCompressor() {
compressor->inputGainDb = 0;
compressor->outputGainDb = 0;
compressor->wet = 1;
compressor->attackSec = 0.003;
compressor->releaseSec = 0.3;
compressor->ratio = 3;
compressor->thresholdDb = 0;
compressor->hpCutOffHz = 1;
compressor->enabled = true;
}
void processAudio(float *interleavedInput, float *interleavedOutput, unsigned int numberOfFrames, unsigned int samplerate) {
compressor->samplerate = samplerate;
bool outputChanged = compressor->process(interleavedInput, interleavedOutput, numberOfFrames);
...
}
Properties
attackSec
PROPERTYAttack in seconds (not milliseconds!).
Type | Min | Max | Default |
---|
Number | 0.0001 | 1 | 0.003 (3ms) |
attackSec
PROPERTYAttack in seconds (not milliseconds!).
Type | Min | Max | Default |
---|
float | 0.0001 | 1 | 0.003 (3ms) |
hpCutOffHz
PROPERTYKey highpass filter frequency.
Type | Min | Max | Default |
---|
Number | 1 | 10000 | 1 |
hpCutOffHz
PROPERTYKey highpass filter frequency.
Type | Min | Max | Default |
---|
float | 1 | 10000 | 1 |
PROPERTYInput gain in decibels.
Type | Min | Max | Default |
---|
Number | -24 | 24 | 0 |
PROPERTYInput gain in decibels.
Type | Min | Max | Default |
---|
float | -24 | 24 | 0 |
outputGainDb
PROPERTYOutput gain in decibels.
Type | Min | Max | Default |
---|
Number | -24 | 24 | 0 |
outputGainDb
PROPERTYOutput gain in decibels.
Type | Min | Max | Default |
---|
float | -24 | 24 | 0 |
ratio
PROPERTYRatio, rounded to 1.5, 2, 3, 4, 5 or 10.
Type | Min | Max | Default |
---|
Number | 1 | 10 | 3 |
ratio
PROPERTYRatio, rounded to 1.5, 2, 3, 4, 5 or 10.
Type | Min | Max | Default |
---|
float | 1 | 10 | 3 |
releaseSec
PROPERTYRelease in seconds (not milliseconds!)
Type | Min | Max | Default |
---|
Number | 0.1 | 4.0 | 0.3 (300ms) |
releaseSec
PROPERTYRelease in seconds (not milliseconds!)
Type | Min | Max | Default |
---|
float | 0.1 | 4.0 | 0.3 (300ms) |
thresholdDb
PROPERTYThreshold in decibels.
Type | Min | Max | Default |
---|
Number | -40 | 0 | 0 |
thresholdDb
PROPERTYThreshold in decibels.
Type | Min | Max | Default |
---|
float | -40 | 0 | 0 |
wet
PROPERTYDry/wet ratio.
Type | Min | Max | Default |
---|
Number | 0 | 1 | 1 |
wet
PROPERTYDry/wet ratio.
Type | Min | Max | Default |
---|
float | 0 | 1 | 1 |
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 Compressor class.
ParametersName | Type | Description |
---|
samplerate | Number | The initial sample rate in Hz. |
ReturnsType | Description |
---|
Superpowered.Compressor | The constructed instance |
constructor
METHODCreates an instance of the Compressor class.
ParametersName | Type | Default | Description |
---|
samplerate | unsigned int | | The initial sample rate in Hz. |
ReturnsType | Description |
---|
Superpowered::Compressor | The constructed instance |
getGainReductionDb
METHODReturns the maximum gain reduction in decibels since the last getGainReductionDb() call.
ParametersNone
ReturnsType | Description |
---|
Number | Reduction in db. |
getGainReductionDb
METHODReturns the maximum gain reduction in decibels since the last getGainReductionDb() call.
ParametersNone
ReturnsType | Description |
---|
float | Reduction in db. |
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). |