Hide navigation sidebar
Hide table of contents sidebar
Skip to content
Back to top
Toggle table of contents sidebar

# Dynamic Range Control

Dynamic Range Control (DRC) in audio digital signal processing (DSP) refers to the automatic adjustment of an audio signal’s amplitude to reduce its dynamic range - the difference between the loudest and quietest parts of the audio. They include compressors, limiters and clippers, as well as the envelope detectors used to detect the signal level.

## Attack and Release Times

Nearly all DRC modules feature an attack and release time to control the responsiveness of the module to changes in signal level. Attack and release times converted from seconds to alpha coefficients for use in the the exponential moving average calculation. The shorter the attack or release time, the bigger the alpha. Large alpha will result in the envelope becoming more reactive to the input samples. Small alpha values will give more smoothed behaviour. The difference between the input level and the current envelope or gain determines whether the attack or release alpha is used.

## Envelope Detectors

Envelope detectors run an exponential moving average (EMA) of the incoming signal. They are used as a part of the most DRC components. They can also be used to implement VU meters and level detectors.

They feature attack and release times to control the responsiveness of the envelope detector.

The C struct below is used for all the envelope detector implementations.

struct env_detector_t

  Envelope detector state structure.

  Public Members

  q1_31 attack_alpha

    Attack alpha

  q1_31 release_alpha

    Release alpha

  int32_t envelope

    Current envelope

### Peak Envelope Detector

A peak-based envelope detector will run its EMA using the absolute value of the input sample.

C API

void adsp_env_detector_peak(env_detector_t *env_det, int32_t new_sample)

  Update the envelope detector peak with a new sample.

  Parameters:

    - env_det – Envelope detector object

    - new_sample – New sample

Python API

class audio_dsp.dsp.drc.envelope_detector_peak(fs, n_chans, attack_t, release_t, Q_sig=27)

  Envelope detector that follows the absolute peak value of a signal.

  The attack time sets how fast the envelope detector ramps up. The release time sets how fast the envelope detector ramps down.

  Parameters:
    fs int

      Sampling frequency in Hz.

    n_chans int

      Number of channels the block runs on.

    attack_t float

      Attack time of the envelope detector in seconds. This cannot be faster than 2/fs seconds, and saturates to that value. Exceptionally large attack times may converge to zero.

    release_t: float

      Release time of the envelope detector in seconds. This cannot be faster than 2/fs seconds, and saturates to that value. Exceptionally large release times may converge to zero.

    Q_sig: int, optional

      Q format of the signal, number of bits after the decimal point. Defaults to Q4.27.

  Attributes:
    fs int

      Sampling frequency in Hz.

    n_chans int

      Number of channels the block runs on.

    Q_sig: int

      Q format of the signal, number of bits after the decimal point.

    `attack_t` float

      The attack time of the compressor/limiter in seconds; changing this property also sets the EWM alpha values for fixed and floating point implementations.

    `release_t` float

      The release time of the compressor/limiter in seconds; changing this property also sets the EWM alpha values for fixed and floating point implementations.

    attack_alpha float

      Attack time parameter used for exponential moving average in floating point processing.

    release_alpha float

      Release time parameter used for exponential moving average in floating point processing.

    envelope list[float]

      Current envelope value for each channel for floating point processing.

    attack_alpha_int int

      attack_alpha in 32-bit int format.

    release_alpha_int int

      release_alpha in 32-bit int format.

    envelope_int list[int]

      current envelope value for each channel in 32-bit int format.

  process(sample, channel=0)

    Update the peak envelope for a signal, using floating point maths.

    Take one new sample and return the updated envelope. Input should be scaled with 0 dB = 1.0.

    Parameters:
      sample float

        The input sample to be processed.

      channel int, optional

        The channel index to process the sample on. Default is 0.

    Returns:
      float

        The processed sample.

  reset_state()

    Reset the envelope to zero.

### RMS Envelope Detector

An RMS-based envelope detector will run its EMA using the square of the input sample. It returns the mean² in order to avoid a square root.

C API

void adsp_env_detector_rms(env_detector_t *env_det, int32_t new_sample)

  Update the envelope detector RMS with a new sample.

  Parameters:

    - env_det – Envelope detector object

    - new_sample – New sample

Python API

class audio_dsp.dsp.drc.envelope_detector_rms(fs, n_chans, attack_t, release_t, Q_sig=27)

  Envelope detector that follows the RMS value of a signal.

  Note this returns the mean² value, there is no need to do the sqrt() as if the output is converted to dB, 10log10() can be taken instead of 20log10().

  The attack time sets how fast the envelope detector ramps up. The release time sets how fast the envelope detector ramps down.

  Parameters:
    fs int

      Sampling frequency in Hz.

    n_chans int

      Number of channels the block runs on.

    attack_t float

      Attack time of the envelope detector in seconds. This cannot be faster than 2/fs seconds, and saturates to that value. Exceptionally large attack times may converge to zero.

    release_t: float

      Release time of the envelope detector in seconds. This cannot be faster than 2/fs seconds, and saturates to that value. Exceptionally large release times may converge to zero.

    Q_sig: int, optional

      Q format of the signal, number of bits after the decimal point. Defaults to Q4.27.

  Attributes:
    fs int

      Sampling frequency in Hz.

    n_chans int

      Number of channels the block runs on.

    Q_sig: int

      Q format of the signal, number of bits after the decimal point.

    `attack_t` float

      The attack time of the compressor/limiter in seconds; changing this property also sets the EWM alpha values for fixed and floating point implementations.

    `release_t` float

      The release time of the compressor/limiter in seconds; changing this property also sets the EWM alpha values for fixed and floating point implementations.

    attack_alpha float

      Attack time parameter used for exponential moving average in floating point processing.

    release_alpha float

      Release time parameter used for exponential moving average in floating point processing.

    envelope list[float]

      Current envelope value for each channel for floating point processing.

    attack_alpha_int int

      attack_alpha in 32-bit int format.

    release_alpha_int int

      release_alpha in 32-bit int format.

    envelope_int list[int]

      current envelope value for each channel in 32-bit int format.

  process(sample, channel=0)

    Update the RMS envelope for a signal, using floating point maths.

    Take one new sample and return the updated envelope. Input should be scaled with 0 dB = 1.0.

    Note this returns the mean² value, there is no need to do the sqrt() as if the output is converted to dB, 10log10() can be taken instead of 20log10().

    Parameters:
      sample float

        The input sample to be processed.

      channel int, optional

        The channel index to process the sample on. Default is 0.

    Returns:
      float

        The processed sample.

  reset_state()

    Reset the envelope to zero.

## Clipper

A clipper limits the signal to a specified threshold. It is applied instantaneously, so has no attack or release times.

typedef int32_t clipper_t

  Clipper state structure. Should be initilised with the linear threshold.

C API

int32_t adsp_clipper(clipper_t clip, int32_t new_samp)

  Process a new sample with a clipper.

  Parameters:

    - clip – Clipper object

    - new_samp – New sample

  Returns:

    int32_t Clipped sample

Python API

class audio_dsp.dsp.drc.clipper(fs, n_chans, threshold_db, Q_sig=27)

  A simple clipper that limits the signal to a specified threshold.

  Parameters:
    fs int

      Sampling frequency in Hz.

    n_chans int

      Number of channels the block runs on.

    threshold_db float

      Threshold above which clipping occurs in dB.

    Q_sig: int, optional

      Q format of the signal, number of bits after the decimal point. Defaults to Q4.27.

  Attributes:
    fs int

      Sampling frequency in Hz.

    n_chans int

      Number of channels the block runs on.

    Q_sig: int

      Q format of the signal, number of bits after the decimal point.

    `threshold_db` float

      The threshold in decibels; changing this property also updates the fixed and floating point thresholds in linear gain.

    threshold float

      Value above which clipping occurs for floating point processing.

    threshold_int int

      Value above which clipping occurs for int32 fixed point processing.

  process(sample, channel=0)

    Take one new sample and return the clipped sample, using floating point maths. Input should be scaled with 0 dB = 1.0.

    Parameters:
      sample float

        The input sample to be processed.

      channel int, optional

        The channel index to process the sample on. Default is 0.

    Returns:
      float

        The processed sample.

## Limiters

Limiters will reduce the amplitude of a signal when the signal envelope is greater than the desired threshold. This is similar behaviour to a compressor with an infinite ratio.

A limiter will run an internal envelope detector to get the signal envelope, then compare it to the threshold. If the envelope is greater than the threshold, the applied gain will be reduced. If the envelope is below the threshold, unity gain will be applied. The gain is run through an EMA to avoid abrupt changes. The same attack and release times are used for the envelope detector and the gain smoothing.

The C struct below is used for all the limiter implementations.

struct limiter_t

  Limiter state structure.

  Public Members

  env_detector_t env_det

    Envelope detector

  int32_t threshold

    Linear threshold

  int32_t gain

    Linear gain

### Peak Limiter

A peak limiter uses the Peak Envelope Detector to get an envelope. When envelope is above the threshold, the new gain is calculated as `threshold / envelope`.

C API

int32_t adsp_limiter_peak(limiter_t *lim, int32_t new_samp)

  Process a new sample with a peak limiter.

  Parameters:

    - lim – Limiter object

    - new_samp – New sample

  Returns:

    int32_t Limited sample

Python API

class audio_dsp.dsp.drc.limiter_peak(fs, n_chans, threshold_db, attack_t, release_t, Q_sig=27)

  A limiter based on the peak value of the signal. When the peak envelope of the signal exceeds the threshold, the signal amplitude is reduced.

  The threshold set the value above which limiting occurs. The attack time sets how fast the limiter starts limiting. The release time sets how long the signal takes to ramp up to its original level after the envelope is below the threshold.

  Parameters:
    fs int

      Sampling frequency in Hz.

    n_chans int

      Number of parallel channels the compressor/limiter runs on. The channels are limited/compressed separately, only the constant parameters are shared.

    threshold_db float

      Threshold in decibels above which limiting occurs.

    attack_t float

      Attack time of the compressor/limiter in seconds. This cannot be faster than 2/fs seconds, and saturates to that value. Exceptionally large attack times may converge to zero.

    release_t: float

      Release time of the compressor/limiter in seconds. This cannot be faster than 2/fs seconds, and saturates to that value. Exceptionally large release times may converge to zero.

    Q_sig: int, optional

      Q format of the signal, number of bits after the decimal point. Defaults to Q4.27.

  Attributes:
    fs int

      Sampling frequency in Hz.

    n_chans int

      Number of channels the block runs on.

    Q_sig: int

      Q format of the signal, number of bits after the decimal point.

    `attack_t` float

      The attack time of the compressor/limiter in seconds; changing this property also sets the EWM alpha values for fixed and floating point implementations.

    `release_t` float

      The release time of the compressor/limiter in seconds; changing this property also sets the EWM alpha values for fixed and floating point implementations.

    threshold float

      Value above which compression/limiting occurs for floating point processing.

    gain list[float]

      Current gain to be applied to the signal for each channel for floating point processing.

    attack_alpha float

      Attack time parameter used for exponential moving average in floating point processing.

    release_alpha float

      Release time parameter used for exponential moving average in floating point processing.

    threshold_int int

      Value above which compression/limiting occurs for int32 fixed point processing.

    gain_int list[int]

      Current gain to be applied to the signal for each channel for int32 fixed point processing.

    attack_alpha_int int

      attack_alpha in 32-bit int format.

    release_alpha_int int

      release_alpha in 32-bit int format.

    gain_calc function

      function pointer to floating point gain calculation function.

    gain_calc_int function

      function pointer to fixed point gain calculation function.

    `threshold_db` float

      The threshold in decibels; changing this property also updates the fixed and floating point thresholds in linear gain.

    env_detector envelope_detector_peak

      Peak envelope detector object used to calculate the envelope of the signal.

  process(sample, channel=0)

    Update the envelope for a signal, then calculate and apply the required gain for compression/limiting, using floating point maths.

    Take one new sample and return the compressed/limited sample. Input should be scaled with 0 dB = 1.0.

    Parameters:
      sample float

        The input sample to be processed.

      channel int, optional

        The channel index to process the sample on. Default is 0.

    Returns:
      float

        The processed sample.

  reset_state()

    Reset the envelope detector to 0 and the gain to 1.

### Hard Peak Limiter

A hard peak limiter is similar to a Peak Limiter, but will clip the output if it’s still above the threshold after the peak limiter. This can be useful for a final output limiter before truncating any headroom bits.

C API

int32_t adsp_hard_limiter_peak(limiter_t *lim, int32_t new_samp)

  Process a new sample with a hard limiter peak.

  Parameters:

    - lim – Limiter object

    - new_samp – New sample

  Returns:

    int32_t Limited sample

Python API

class audio_dsp.dsp.drc.hard_limiter_peak(fs, n_chans, threshold_db, attack_t, release_t, Q_sig=27)

  A limiter based on the peak value of the signal. When the peak envelope of the signal exceeds the threshold, the signal amplitude is reduced. If the signal still exceeds the threshold, it is clipped.

  The threshold set the value above which limiting/clipping occurs. The attack time sets how fast the limiter starts limiting. The release time sets how long the signal takes to ramp up to it’s original level after the envelope is below the threshold.

  Parameters:
    fs int

      Sampling frequency in Hz.

    n_chans int

      Number of parallel channels the compressor/limiter runs on. The channels are limited/compressed separately, only the constant parameters are shared.

    threshold_db float

      Threshold in decibels above which limiting occurs.

    attack_t float

      Attack time of the compressor/limiter in seconds. This cannot be faster than 2/fs seconds, and saturates to that value. Exceptionally large attack times may converge to zero.

    release_t: float

      Release time of the compressor/limiter in seconds. This cannot be faster than 2/fs seconds, and saturates to that value. Exceptionally large release times may converge to zero.

    Q_sig: int, optional

      Q format of the signal, number of bits after the decimal point. Defaults to Q4.27.

  Attributes:
    fs int

      Sampling frequency in Hz.

    n_chans int

      Number of channels the block runs on.

    Q_sig: int

      Q format of the signal, number of bits after the decimal point.

    `attack_t` float

      The attack time of the compressor/limiter in seconds; changing this property also sets the EWM alpha values for fixed and floating point implementations.

    `release_t` float

      The release time of the compressor/limiter in seconds; changing this property also sets the EWM alpha values for fixed and floating point implementations.

    threshold float

      Value above which compression/limiting occurs for floating point processing.

    gain list[float]

      Current gain to be applied to the signal for each channel for floating point processing.

    attack_alpha float

      Attack time parameter used for exponential moving average in floating point processing.

    release_alpha float

      Release time parameter used for exponential moving average in floating point processing.

    threshold_int int

      Value above which compression/limiting occurs for int32 fixed point processing.

    gain_int list[int]

      Current gain to be applied to the signal for each channel for int32 fixed point processing.

    attack_alpha_int int

      attack_alpha in 32-bit int format.

    release_alpha_int int

      release_alpha in 32-bit int format.

    gain_calc function

      function pointer to floating point gain calculation function.

    gain_calc_int function

      function pointer to fixed point gain calculation function.

    `threshold_db` float

      The threshold in decibels; changing this property also updates the fixed and floating point thresholds in linear gain.

    env_detector envelope_detector_peak

      Peak envelope detector object used to calculate the envelope of the signal.

  process(sample, channel=0)

    Update the envelope for a signal, then calculate and apply the required gain for limiting, using floating point maths. If the output signal exceeds the threshold, clip it to the threshold.

    Take one new sample and return the limited sample. Input should be scaled with 0 dB = 1.0.

    Parameters:
      sample float

        The input sample to be processed.

      channel int, optional

        The channel index to process the sample on. Default is 0.

    Returns:
      float

        The processed sample.

  reset_state()

    Reset the envelope detector to 0 and the gain to 1.

### RMS Limiter

A RMS limiter uses the RMS Envelope Detector to calculate an envelope. When envelope is above the threshold, the new gain is calculated as `sqrt(threshold / envelope)`.

C API

int32_t adsp_limiter_rms(limiter_t *lim, int32_t new_samp)

  Process a new sample with an RMS limiter.

  Parameters:

    - lim – Limiter object

    - new_samp – New sample

  Returns:

    int32_t Limited sample

Python API

class audio_dsp.dsp.drc.limiter_rms(fs, n_chans, threshold_db, attack_t, release_t, Q_sig=27)

  A limiter based on the RMS value of the signal. When the RMS envelope of the signal exceeds the threshold, the signal amplitude is reduced.

  The threshold set the value above which limiting occurs. The attack time sets how fast the limiter starts limiting. The release time sets how long the signal takes to ramp up to its original level after the envelope is below the threshold.

  Parameters:
    fs int

      Sampling frequency in Hz.

    n_chans int

      Number of parallel channels the compressor/limiter runs on. The channels are limited/compressed separately, only the constant parameters are shared.

    threshold_db float

      Threshold in decibels above which limiting occurs.

    attack_t float

      Attack time of the compressor/limiter in seconds. This cannot be faster than 2/fs seconds, and saturates to that value. Exceptionally large attack times may converge to zero.

    release_t: float

      Release time of the compressor/limiter in seconds. This cannot be faster than 2/fs seconds, and saturates to that value. Exceptionally large release times may converge to zero.

    Q_sig: int, optional

      Q format of the signal, number of bits after the decimal point. Defaults to Q4.27.

  Attributes:
    fs int

      Sampling frequency in Hz.

    n_chans int

      Number of channels the block runs on.

    Q_sig: int

      Q format of the signal, number of bits after the decimal point.

    `attack_t` float

      The attack time of the compressor/limiter in seconds; changing this property also sets the EWM alpha values for fixed and floating point implementations.

    `release_t` float

      The release time of the compressor/limiter in seconds; changing this property also sets the EWM alpha values for fixed and floating point implementations.

    threshold float

      Value above which compression/limiting occurs for floating point processing. Note the threshold is saved in the power domain, as the RMS envelope detector returns x².

    gain list[float]

      Current gain to be applied to the signal for each channel for floating point processing.

    attack_alpha float

      Attack time parameter used for exponential moving average in floating point processing.

    release_alpha float

      Release time parameter used for exponential moving average in floating point processing.

    threshold_int int

      Value above which compression/limiting occurs for int32 fixed point processing.

    gain_int list[int]

      Current gain to be applied to the signal for each channel for int32 fixed point processing.

    attack_alpha_int int

      attack_alpha in 32-bit int format.

    release_alpha_int int

      release_alpha in 32-bit int format.

    gain_calc function

      function pointer to floating point gain calculation function.

    gain_calc_int function

      function pointer to fixed point gain calculation function.

    env_detector envelope_detector_rms

      RMS envelope detector object used to calculate the envelope of the signal.

  process(sample, channel=0)

    Update the envelope for a signal, then calculate and apply the required gain for compression/limiting, using floating point maths.

    Take one new sample and return the compressed/limited sample. Input should be scaled with 0 dB = 1.0.

    Parameters:
      sample float

        The input sample to be processed.

      channel int, optional

        The channel index to process the sample on. Default is 0.

    Returns:
      float

        The processed sample.

  reset_state()

    Reset the envelope detector to 0 and the gain to 1.

## Compressors

A compressor will attenuate the signal when the envelope is greater than the threshold. The input/output relationship above the threshold is defined by the compressor `ratio`.

As with a limiter, the compressor runs an internal envelope detector to get the signal envelope, then compares it to the threshold. If the envelope is greater than the threshold, the gain will be proportionally reduced by the `ratio`, such that it is greater than the threshold by a smaller amount. If the envelope is below the threshold, unity gain will be applied. The gain is then run through an EMA to avoid abrupt changes, before being applied.

The `ratio` defines the input/output gradient in the logarithmic domain. For example, a ratio of 2 will reduce the output gain by 0.5 dB for every 1 dB the envelope is over the threshold. A ratio of 1 will apply no compression. To avoid converting the envelope to the logarithmic domain for the gain calculation, the ratio is converted to the `slope` as `(1 - 1 / ratio) / 2` . The gain can then be calculated as an exponential in the linear domain.

The C structs below are used for all the compressors implementations.

struct compressor_t

  Compressor state structure.

  Public Members

  env_detector_t env_det

    Envelope detector

  int32_t threshold

    Linear threshold

  int32_t gain

    Linear gain

  float slope

    Slope of the compression curve

struct compressor_stereo_t

  Stereo compressor state structure.

  Public Members

  env_detector_t env_det_l

    Envelope detector for left channel

  env_detector_t env_det_r

    Envelope detector for right channel

  int32_t threshold

    Linear threshold

  int32_t gain

    Linear gain

  float slope

    Slope of the compression curve

### RMS Compressor

The RMS compressor uses the RMS Envelope Detector to calculate an envelope. When the envelope is above the threshold, the new gain is calculated as `(threshold / envelope) ^ slope`.

C API

int32_t adsp_compressor_rms(compressor_t *comp, int32_t new_samp)

  Process a new sample with an RMS compressor.

  Parameters:

    - comp – Compressor object

    - new_samp – New sample

  Returns:

    int32_t Compressed sample

Python API

class audio_dsp.dsp.drc.compressor_rms(fs, n_chans, ratio, threshold_db, attack_t, release_t, Q_sig=27)

  A compressor based on the RMS value of the signal. When the RMS envelope of the signal exceeds the threshold, the signal amplitude is reduced by the compression ratio.

  The threshold sets the value above which compression occurs. The ratio sets how much the signal is compressed. A ratio of 1 results in no compression, while a ratio of infinity results in the same behaviour as a limiter. The attack time sets how fast the compressor starts compressing. The release time sets how long the signal takes to ramp up to it’s original level after the envelope is below the threshold.

  Parameters:
    fs int

      Sampling frequency in Hz.

    n_chans int

      Number of parallel channels the compressor/limiter runs on. The channels are limited/compressed separately, only the constant parameters are shared.

    ratio float

      Compression gain ratio applied when the signal is above the threshold

    threshold_db float

      Threshold in decibels above which limiting occurs.

    attack_t float

      Attack time of the compressor/limiter in seconds. This cannot be faster than 2/fs seconds, and saturates to that value. Exceptionally large attack times may converge to zero.

    release_t: float

      Release time of the compressor/limiter in seconds. This cannot be faster than 2/fs seconds, and saturates to that value. Exceptionally large release times may converge to zero.

    Q_sig: int, optional

      Q format of the signal, number of bits after the decimal point. Defaults to Q4.27.

  Attributes:
    fs int

      Sampling frequency in Hz.

    n_chans int

      Number of channels the block runs on.

    Q_sig: int

      Q format of the signal, number of bits after the decimal point.

    `attack_t` float

      The attack time of the compressor/limiter in seconds; changing this property also sets the EWM alpha values for fixed and floating point implementations.

    `release_t` float

      The release time of the compressor/limiter in seconds; changing this property also sets the EWM alpha values for fixed and floating point implementations.

    threshold float

      Value above which compression/limiting occurs for floating point processing. Note the threshold is saved in the power domain, as the RMS envelope detector returns x².

    gain list[float]

      Current gain to be applied to the signal for each channel for floating point processing.

    attack_alpha float

      Attack time parameter used for exponential moving average in floating point processing.

    release_alpha float

      Release time parameter used for exponential moving average in floating point processing.

    threshold_int int

      Value above which compression/limiting occurs for int32 fixed point processing.

    gain_int list[int]

      Current gain to be applied to the signal for each channel for int32 fixed point processing.

    attack_alpha_int int

      attack_alpha in 32-bit int format.

    release_alpha_int int

      release_alpha in 32-bit int format.

    gain_calc function

      function pointer to floating point gain calculation function.

    gain_calc_int function

      function pointer to fixed point gain calculation function.

    env_detector envelope_detector_rms

      RMS envelope detector object used to calculate the envelope of the signal.

    `ratio` float

      Compression gain ratio applied when the signal is above the threshold; changing this property also updates the slope used in the fixed and floating point implementation.

    slope float

      The slope factor of the compressor, defined as slope = (1 - 1/ratio) / 2.

    slope_f32 float32

      The slope factor of the compressor, used for int32 to float32 processing.

  process(sample, channel=0)

    Update the envelope for a signal, then calculate and apply the required gain for compression/limiting, using floating point maths.

    Take one new sample and return the compressed/limited sample. Input should be scaled with 0 dB = 1.0.

    Parameters:
      sample float

        The input sample to be processed.

      channel int, optional

        The channel index to process the sample on. Default is 0.

    Returns:
      float

        The processed sample.

  reset_state()

    Reset the envelope detector to 0 and the gain to 1.

### Sidechain RMS Compressor

The sidechain RMS compressor calculates the envelope of one signal and uses it to compress another signal. It takes two signals: detect and input. The envelope of the detect signal is calculated using an internal RMS Envelope Detector. The gain is calculated in the same way as a RMS Compressor, but the gain is then applied to the input sample. This can be used to reduce the level of the input signal when the detect signal gets above the threshold.

C API

int32_t adsp_compressor_rms_sidechain(

  compressor_t *comp,
  int32_t input_samp,
  int32_t detect_samp,

)

  Process a new sample with a sidechain RMS compressor.

  Parameters:

    - comp – Compressor object

    - input_samp – Input sample

    - detect_samp – Sidechain sample

  Returns:

    int32_t Compressed sample

Python API

class audio_dsp.dsp.drc.compressor_rms_sidechain_mono(

  fs,
  ratio,
  threshold_db,
  attack_t,
  release_t,
  Q_sig=27,

)

  A mono sidechain compressor based on the RMS value of the signal. When the RMS envelope of the signal exceeds the threshold, the signal amplitude is reduced by the compression ratio.

  The threshold sets the value above which compression occurs. The ratio sets how much the signal is compressed. A ratio of 1 results in no compression, while a ratio of infinity results in the same behaviour as a limiter. The attack time sets how fast the compressor starts compressing. The release time sets how long the signal takes to ramp up to it’s original level after the envelope is below the threshold.

  Parameters:
    fs int

      Sampling frequency in Hz.

    ratio float

      Compression gain ratio applied when the signal is above the threshold

    threshold_db float

      Threshold in decibels above which limiting occurs.

    attack_t float

      Attack time of the compressor/limiter in seconds. This cannot be faster than 2/fs seconds, and saturates to that value. Exceptionally large attack times may converge to zero.

    release_t: float

      Release time of the compressor/limiter in seconds. This cannot be faster than 2/fs seconds, and saturates to that value. Exceptionally large release times may converge to zero.

    Q_sig: int, optional

      Q format of the signal, number of bits after the decimal point. Defaults to Q4.27.

  Attributes:
    fs int

      Sampling frequency in Hz.

    n_chans int

      Number of channels the block runs on.

    Q_sig: int

      Q format of the signal, number of bits after the decimal point.

    `attack_t` float

      The attack time of the compressor/limiter in seconds; changing this property also sets the EWM alpha values for fixed and floating point implementations.

    `release_t` float

      The release time of the compressor/limiter in seconds; changing this property also sets the EWM alpha values for fixed and floating point implementations.

    threshold float

      Value above which compression/limiting occurs for floating point processing. Note the threshold is saved in the power domain, as the RMS envelope detector returns x².

    gain list[float]

      Current gain to be applied to the signal for each channel for floating point processing.

    attack_alpha float

      Attack time parameter used for exponential moving average in floating point processing.

    release_alpha float

      Release time parameter used for exponential moving average in floating point processing.

    threshold_int int

      Value above which compression/limiting occurs for int32 fixed point processing.

    gain_int list[int]

      Current gain to be applied to the signal for each channel for int32 fixed point processing.

    attack_alpha_int int

      attack_alpha in 32-bit int format.

    release_alpha_int int

      release_alpha in 32-bit int format.

    gain_calc function

      function pointer to floating point gain calculation function.

    gain_calc_int function

      function pointer to fixed point gain calculation function.

    env_detector envelope_detector_rms

      RMS envelope detector object used to calculate the envelope of the signal.

    `ratio` float

      Compression gain ratio applied when the signal is above the threshold; changing this property also updates the slope used in the fixed and floating point implementation.

    slope float

      The slope factor of the compressor, defined as slope = (1 - 1/ratio) / 2.

    slope_f32 float32

      The slope factor of the compressor, used for int32 to float32 processing.

  process(input_sample: float, detect_sample: float)

    Update the envelope for the detection signal, then calculate and apply the required gain for compression/limiting, and apply to the input signal using floating point maths.

    Take one new sample and return the compressed/limited sample. Input should be scaled with 0 dB = 1.0.

    Parameters:
      input_sample float

        The input sample to be compressed.

      detect_sample float

        The sample used by the envelope detector to determine the amount of compression to apply to the input_sample.

    Returns:
      float

        The processed sample.

  reset_state()

    Reset the envelope detectors to 0 and the gain to 1.

### Stereo Sidechain RMS Compressor

The stereo sidechain RMS compressor expands the Sidechain RMS Compressor to take 2 input and 2 detection channels. The envelope of each detection channel is taken, and the maximum of the envelopes is used to calculate the compression to apply to the input channels.

C API

void adsp_compressor_rms_sidechain_stereo(

  compressor_stereo_t *comp,
  int32_t outputs_lr[2],
  int32_t input_samp_l,
  int32_t input_samp_r,
  int32_t detect_samp_l,
  int32_t detect_samp_r,

)

  Process a pair of new samples with a stereo sidechain RMS compressor.

  Parameters:

    - comp – Compressor object

    - outputs_lr – Pointer to the outputs 0:left, 1:right

    - input_samp_l – Left input sample

    - input_samp_r – Right input sample

    - detect_samp_l – Left sidechain sample

    - detect_samp_r – Right sidechain sample

Python API

class audio_dsp.dsp.drc.compressor_rms_sidechain_stereo(

  fs,
  ratio,
  threshold_db,
  attack_t,
  release_t,
  Q_sig=27,

)

  A stereo sidechain compressor based on the RMS value of the signal. When the RMS envelope of the signal exceeds the threshold, the signal amplitude is reduced by the compression ratio. The same compression is applied to both channels, using the highest individual channel envelope.

  The threshold sets the value above which compression occurs. The ratio sets how much the signal is compressed. A ratio of 1 results in no compression, while a ratio of infinity results in the same behaviour as a limiter. The attack time sets how fast the compressor starts compressing. The release time sets how long the signal takes to ramp up to it’s original level after the envelope is below the threshold.

  Parameters:
    fs int

      Sampling frequency in Hz.

    ratio float

      Compression gain ratio applied when the signal is above the threshold

    threshold_db float

      Threshold in decibels above which limiting occurs.

    attack_t float

      Attack time of the compressor/limiter in seconds. This cannot be faster than 2/fs seconds, and saturates to that value. Exceptionally large attack times may converge to zero.

    release_t: float

      Release time of the compressor/limiter in seconds. This cannot be faster than 2/fs seconds, and saturates to that value. Exceptionally large release times may converge to zero.

    Q_sig: int, optional

      Q format of the signal, number of bits after the decimal point. Defaults to Q4.27.

  Attributes:
    fs int

      Sampling frequency in Hz.

    n_chans int

      Number of channels the block runs on.

    Q_sig: int

      Q format of the signal, number of bits after the decimal point.

    `threshold_db` float

      The threshold in decibels; changing this property also updates the fixed and floating point thresholds in linear gain.

    `attack_t` float

      The attack time in seconds.

    `release_t` float

      The release time in seconds.

    threshold float

      Value above which compression/limiting occurs for floating point processing. Note the threshold is saved in the power domain, as the RMS envelope detector returns x².

    gain list[float]

      Current gain to be applied to the signal for each channel for floating point processing.

    attack_alpha float

      Attack time parameter used for exponential moving average in floating point processing.

    release_alpha float

      Release time parameter used for exponential moving average in floating point processing.

    threshold_int int

      Value above which compression/limiting occurs for int32 fixed point processing.

    gain_int list[int]

      Current gain to be applied to the signal for each channel for int32 fixed point processing.

    attack_alpha_int int

      attack_alpha in 32-bit int format.

    release_alpha_int int

      release_alpha in 32-bit int format.

    gain_calc function

      function pointer to floating point gain calculation function.

    gain_calc_int function

      function pointer to fixed point gain calculation function.

    env_detector envelope_detector_rms

      RMS envelope detector object used to calculate the envelope of the signal.

    `ratio` float

      Compression gain ratio applied when the signal is above the threshold; changing this property also updates the slope used in the fixed and floating point implementation.

    slope float

      The slope factor of the compressor, defined as slope = (1 - 1/ratio).

    slope_f32 float32

      The slope factor of the compressor, used for int32 to float32 processing.

  process(sample: float, channel=0)

    Take one new sample and give it back. Do no processing for the generic block.

    Parameters:
      sample float

        The input sample to be processed.

      channel int, optional

        The channel index to process the sample on. Default is 0.

    Returns:
      float

        The processed sample.

  reset_state()

    Reset the envelope detectors to 0 and the gain to 1.

## Expanders

An expander attenuates a signal when the envelope is below the threshold. This increases the dynamic range of the signal, and can be used to attenuate quiet signals, such as low level noise.

Like limiters and compressors, an expander will run an internal envelope detector to calculate the envelope and compare it to the threshold. If the envelope is below the threshold, the applied gain will be reduced. If the envelope is greater than the threshold, unity gain will be applied. The gain is run through an EMA to avoid abrupt changes. The same attack and release times are used for the envelope detector and the gain smoothing. In an expander, the attack time is defined as the speed at which the gain returns to unity after the signal has been below the threshold.

### Noise Gate

A noise gate uses the Peak Envelope Detector to calculate the envelope of the input signal. When the envelope is below the threshold, a gain of 0 is applied to the input signal. Otherwise, unity gain is applied.

typedef limiter_t noise_gate_t

  Noise gate state structure.

C API

int32_t adsp_noise_gate(noise_gate_t *ng, int32_t new_samp)

  Process a new sample with a noise gate.

  Parameters:

    - ng – Noise gate object

    - new_samp – New sample

  Returns:

    int32_t Gated sample

Python API

class audio_dsp.dsp.drc.noise_gate(fs, n_chans, threshold_db, attack_t, release_t, Q_sig=27)

  A noise gate that reduces the level of an audio signal when it falls below a threshold.

  When the signal envelope falls below the threshold, the gain applied to the signal is reduced to 0 (based on the release time). When the envelope returns above the threshold, the gain applied to the signal is increased to 1 over the attack time.

  The initial state of the noise gate is with the gate open (no attenuation), assuming a full scale signal has been present before t = 0.

  Parameters:
    fs int

      Sampling frequency in Hz.

    n_chans int

      number of parallel channels the expander runs on. The channels are expanded separately, only the constant parameters are shared.

    threshold_db float

      Threshold in decibels below which expansion occurs. This cannot be greater than the maximum value representable in Q_SIG format, and will saturate to that value.

    attack_t float

      Attack time of the expander in seconds. This cannot be faster than 2/fs seconds, and saturates to that value. Exceptionally large attack times may converge to zero.

    release_t: float

      Release time of the expander in seconds. This cannot be faster than 2/fs seconds, and saturates to that value. Exceptionally large release times may converge to zero.

    Q_sig: int, optional

      Q format of the signal, number of bits after the decimal point. Defaults to Q4.27.

  Attributes:
    fs int

      Sampling frequency in Hz.

    n_chans int

      Number of channels the block runs on.

    Q_sig: int

      Q format of the signal, number of bits after the decimal point.

    `attack_t` float

      The attack time of the compressor/limiter in seconds; changing this property also sets the EWM alpha values for fixed and floating point implementations.

    `release_t` float

      The release time of the compressor/limiter in seconds; changing this property also sets the EWM alpha values for fixed and floating point implementations.

    threshold float

      Value below which expanding occurs for floating point processing.

    gain list[float]

      Current gain to be applied to the signal for each channel for floating point processing.

    attack_alpha float

      Attack time parameter used for exponential moving average in floating point processing.

    release_alpha float

      Release time parameter used for exponential moving average in floating point processing.

    threshold_int int

      Value below which expanding occurs for int32 fixed point processing.

    gain_int list[int]

      Current gain to be applied to the signal for each channel for int32 fixed point processing.

    attack_alpha_int int

      attack_alpha in 32-bit int format.

    release_alpha_int int

      release_alpha in 32-bit int format.

    gain_calc function

      function pointer to floating point gain calculation function.

    gain_calc_int function

      function pointer to fixed point gain calculation function.

    `threshold_db` float

      The threshold in decibels; changing this property also updates the fixed and floating point thresholds in linear gain.

    env_detector envelope_detector_peak

      Peak envelope detector object used to calculate the envelope of the signal.

  process(sample, channel=0)

    Update the envelope for a signal, then calculate and apply the required gain for expanding, using floating point maths.

    Take one new sample and return the expanded sample. Input should be scaled with 0 dB = 1.0.

    Parameters:
      sample float

        The input sample to be processed.

      channel int, optional

        The channel index to process the sample on. Default is 0.

    Returns:
      float

        The processed sample.

  reset_state()

    Reset the envelope detector to 1 and the gain to 1, so the gate starts off.

### Noise Suppressor/Expander

A basic expander can also be used as a noise suppressor. It uses the Peak Envelope Detector to calculate the envelope of the input signal. When the envelope is below the threshold, the gain of the signal is reduced according to the ratio. Otherwise, unity gain is applied.

Like a compressor, the `ratio` defines the input/output gradient in the logarithmic domain. For example, a ratio of 2 will reduce the output gain by 0.5 dB for every 1 dB the envelope is below the threshold. A ratio of 1 will apply no gain changes. To avoid converting the envelope to the logarithmic domain for the gain calculation, the ratio is converted to the `slope` as `(1 - ratio)`. The gain can then be calculated as an exponential in the linear domain.

For speed, some parameters such as `inv_threshold` are computed at initialisation to simplify run-time computation.

struct noise_suppressor_expander_t

  Public Members

  env_detector_t env_det

    Envelope detector

  int32_t threshold

    Linear threshold

  int64_t inv_threshold

    Inverse threshold

  int32_t gain

    Linear gain

  float slope

    Slope of the noise suppression curve

C API

int32_t adsp_noise_suppressor_expander(

  noise_suppressor_expander_t *nse,
  int32_t new_samp,

)

  Process a new sample with a noise suppressor (expander)

  Parameters:

    - nse – Noise suppressor (Expander) object

    - new_samp – New sample

  Returns:

    int32_t Suppressed sample

Python API

class audio_dsp.dsp.drc.noise_suppressor_expander(

  fs,
  n_chans,
  ratio,
  threshold_db,
  attack_t,
  release_t,
  Q_sig=27,

)

  A noise suppressor that reduces the level of an audio signal when it falls below a threshold. This is also known as an expander.

  When the signal envelope falls below the threshold, the gain applied to the signal is reduced relative to the expansion ratio over the release time. When the envelope returns above the threshold, the gain applied to the signal is increased to 1 over the attack time.

  The initial state of the noise suppressor is with the suppression off, assuming a full scale signal has been present before t = 0.

  Parameters:
    fs int

      Sampling frequency in Hz.

    n_chans int

      number of parallel channels the expander runs on. The channels are expanded separately, only the constant parameters are shared.

    ratio float

      The expansion ratio applied to the signal when the envelope falls below the threshold.

    threshold_db float

      Threshold in decibels below which expansion occurs. This cannot be greater than the maximum value representable in Q_SIG format, and will saturate to that value.

    attack_t float

      Attack time of the expander in seconds. This cannot be faster than 2/fs seconds, and saturates to that value. Exceptionally large attack times may converge to zero.

    release_t: float

      Release time of the expander in seconds. This cannot be faster than 2/fs seconds, and saturates to that value. Exceptionally large release times may converge to zero.

    Q_sig: int, optional

      Q format of the signal, number of bits after the decimal point. Defaults to Q4.27.

  Attributes:
    fs int

      Sampling frequency in Hz.

    n_chans int

      Number of channels the block runs on.

    Q_sig: int

      Q format of the signal, number of bits after the decimal point.

    `attack_t` float

      The attack time of the compressor/limiter in seconds; changing this property also sets the EWM alpha values for fixed and floating point implementations.

    `release_t` float

      The release time of the compressor/limiter in seconds; changing this property also sets the EWM alpha values for fixed and floating point implementations.

    threshold float

      Value below which expanding occurs for floating point processing.

    gain list[float]

      Current gain to be applied to the signal for each channel for floating point processing.

    attack_alpha float

      Attack time parameter used for exponential moving average in floating point processing.

    release_alpha float

      Release time parameter used for exponential moving average in floating point processing.

    threshold_int int

      Value below which expanding occurs for int32 fixed point processing.

    gain_int list[int]

      Current gain to be applied to the signal for each channel for int32 fixed point processing.

    attack_alpha_int int

      attack_alpha in 32-bit int format.

    release_alpha_int int

      release_alpha in 32-bit int format.

    gain_calc function

      function pointer to floating point gain calculation function.

    gain_calc_int function

      function pointer to fixed point gain calculation function.

    `threshold_db` float

      The threshold in decibels; changing this property also updates the fixed and floating point thresholds in linear gain.

    env_detector envelope_detector_peak

      Peak envelope detector object used to calculate the envelope of the signal.

    `ratio` float

      Expansion gain ratio applied when the signal is below the threshold; changing this property also updates the slope used in the fixed and floating point implementation.

    slope float

      The slope factor of the expander, defined as slope = 1 - ratio.

    slope_f32 float32

      The slope factor of the expander, used for int32 to float32 processing.

  process(sample, channel=0)

    Update the envelope for a signal, then calculate and apply the required gain for expanding, using floating point maths.

    Take one new sample and return the expanded sample. Input should be scaled with 0 dB = 1.0.

    Parameters:
      sample float

        The input sample to be processed.

      channel int, optional

        The channel index to process the sample on. Default is 0.

    Returns:
      float

        The processed sample.

  reset_state()

    Reset the envelope detector to 1 and the gain to 1, so the gate starts off.