3#include <dsplib/array.h>
4#include <dsplib/utils.h>
5#include <dsplib/math.h>
32 wA_ = std::exp(-std::log(9) / (sample_rate *
attack_time));
33 wR_ = std::exp(-std::log(9) / (sample_rate *
release_time));
34 DSPLIB_ASSERT(
threshold >= -50 &&
threshold <= 0,
"`threshold` must be in range [-50:0] db");
35 DSPLIB_ASSERT(
ratio >= 1 &&
ratio <= 50,
"`ratio` must be in range [1:50]");
62 const int n =
x.size();
64 for (
int i = 0;
i <
n; ++
i) {
65 const auto gc = _compute_gain(
x[
i]);
67 gs_ = (wA_ * gs_) + (1 - wA_) *
gc;
69 gs_ = (wR_ * gs_) + (1 - wR_) *
gc;
71 const auto glin = db2mag(gs_);
87 [[nodiscard]] real_t _compute_gain(
const real_t& x)
const noexcept {
89 const auto xdb = mag2db(dsplib::abs(x) + eps());
91 if (xdb >= (T_ + W_ / 2)) {
92 xsc = T_ + (xdb - T_) / R_;
93 }
else if ((xdb > (T_ - W_ / 2)) && (xdb < (T_ + W_ / 2))) {
94 xsc = xdb + (1 / R_ - 1) * abs2(xdb - T_ + W_ / 2) / (2 * W_);
Dynamic range compressor.
Definition compressor.h:15
Result operator()(span_real x)
Process audio frame, obj(x) syntax.
Definition compressor.h:82
Compressor(int sample_rate=44100, real_t threshold=-10.0, int ratio=5, real_t knee_width=0, real_t attack_time=0.01, real_t release_time=0.2)
Construct a new Compressor object.
Definition compressor.h:27
Result process(span_real x)
Process audio frame.
Definition compressor.h:61
Compressor processing result.
Definition compressor.h:45
arr_real out
processed signal, in * gain
Definition compressor.h:51
arr_real gain
applied gain
Definition compressor.h:52