Skip to content

File compressor.h

File List > DaisySP > DaisySP-LGPL > Source > Dynamics > compressor.h

Go to the documentation of this file

Source Code

/*
Copyright (c) 2023 Electrosmith, Corp, GRAME, Centre National de Creation Musicale.

Use of this source code is governed by the LGPL V2.1
license that can be found in the LICENSE file or at
https://opensource.org/license/lgpl-2-1/
*/

#pragma once
#ifndef DSY_COMPRESSOR_H
#define DSY_COMPRESSOR_H

#include "Utility/dsp.h"

namespace daisysp
{
class Compressor
{
  public:
    Compressor() {}
    ~Compressor() {}
    void Init(float sample_rate);

    float Process(float in);

    float Process(float in, float key)
    {
        Process(key);
        return Apply(in);
    }

    float Apply(float in) { return gain_ * in; }

    void ProcessBlock(float *in, float *out, size_t size)
    {
        ProcessBlock(in, out, in, size);
    }

    void ProcessBlock(float *in, float *out, float *key, size_t size);

    void ProcessBlock(float **in,
                      float **out,
                      float * key,
                      size_t  channels,
                      size_t  size);

    float GetRatio() { return ratio_; }

    void SetRatio(float ratio)
    {
        ratio_ = ratio;
        RecalculateRatio();
    }

    float GetThreshold() { return thresh_; }

    void SetThreshold(float threshold)
    {
        thresh_ = threshold;
        RecalculateMakeup();
    }

    float GetAttack() { return atk_; }

    void SetAttack(float attack)
    {
        atk_ = attack;
        RecalculateAttack();
    }

    float GetRelease() { return rel_; }

    void SetRelease(float release)
    {
        rel_ = release;
        RecalculateRelease();
    }

    float GetMakeup() { return makeup_gain_; }

    void SetMakeup(float gain) { makeup_gain_ = gain; }

    void AutoMakeup(bool enable)
    {
        makeup_auto_ = enable;
        makeup_gain_ = 0.0f;
        RecalculateMakeup();
    }

    float GetGain() { return fastlog10f(gain_) * 20.0f; }

  private:
    float ratio_, thresh_, atk_, rel_;
    float makeup_gain_;
    float gain_;

    // Recorded slope and gain, used in next sample
    float slope_rec_, gain_rec_;

    // Internals from faust
    float atk_slo2_, ratio_mul_, atk_slo_, rel_slo_;

    int   sample_rate_;
    float sample_rate_inv2_, sample_rate_inv_;

    // Auto makeup gain enable
    bool makeup_auto_;

    // Methods for recalculating internals
    void RecalculateRatio()
    {
        ratio_mul_ = ((1.0f - atk_slo2_) * ((1.0f / ratio_) - 1.0f));
    }

    void RecalculateAttack()
    {
        atk_slo_  = expf(-(sample_rate_inv_ / atk_));
        atk_slo2_ = expf(-(sample_rate_inv2_ / atk_));

        RecalculateRatio();
    }

    void RecalculateRelease() { rel_slo_ = expf((-(sample_rate_inv_ / rel_))); }

    void RecalculateMakeup()
    {
        if(makeup_auto_)
            makeup_gain_ = fabsf(thresh_ - thresh_ / ratio_) * 0.5f;
    }
};

} // namespace daisysp

#endif // DSY_COMPRESSOR_H