Skip to content

Commit 061aac2

Browse files
committed
fix(knee computer): fix output for sample larger than zero
1 parent a8e2fbb commit 061aac2

1 file changed

Lines changed: 50 additions & 29 deletions

File tree

source/dsp/compressor/computer/knee_computer.hpp

Lines changed: 50 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,22 @@ namespace zldsp::compressor {
3939
high_th_ = other.high_th_;
4040
para_mid_g0_ = other.para_mid_g0_;
4141
para_high_g0_ = other.para_high_g0_;
42+
para_over_g0_ = other.para_over_g0_;
4243
}
4344

4445
FloatType eval(FloatType x) override {
4546
if (x <= low_th_) {
46-
return OutputDiff ? FloatType(0) : x;
47-
} else if (x >= high_th_) {
48-
x = std::min(x, FloatType(0));
47+
if constexpr (OutputDiff) {
48+
return FloatType(0);
49+
} else {
50+
return x;
51+
}
52+
} else if (x < high_th_) {
53+
return (para_mid_g0_[0] * x + para_mid_g0_[1]) * x + para_mid_g0_[2];
54+
} else if (x < FloatType(0)) {
4955
return (para_high_g0_[0] * x + para_high_g0_[1]) * x + para_high_g0_[2];
5056
} else {
51-
return (para_mid_g0_[0] * x + para_mid_g0_[1]) * x + para_mid_g0_[2];
57+
return para_over_g0_[0] * x + para_over_g0_[1];
5258
}
5359
}
5460

@@ -86,7 +92,7 @@ namespace zldsp::compressor {
8692

8793
void setPara(FloatType t, FloatType r, FloatType) {
8894
b = FloatType(1) / r;
89-
c = t * (FloatType(1) - FloatType(1) / r);
95+
c = t * (FloatType(1) - b);
9096
}
9197
};
9298

@@ -95,8 +101,9 @@ namespace zldsp::compressor {
95101
static constexpr FloatType b{FloatType(0)};
96102

97103
void setPara(FloatType t, FloatType r, FloatType w) {
98-
a = FloatType(0.5) / (r * std::min(t + w, FloatType(-0.0001)));
99-
c = FloatType(0.5) * (w - t) / r + t;
104+
const auto temp = FloatType(0.5) / r;
105+
a = temp / std::min(t + w, FloatType(-0.0001));
106+
c = temp * (w - t) + t;
100107
}
101108
};
102109

@@ -105,8 +112,9 @@ namespace zldsp::compressor {
105112
static constexpr FloatType b{FloatType(1)};
106113

107114
void setPara(FloatType t, FloatType r, FloatType w) {
108-
a = FloatType(0.5) * (FloatType(1) - r) / (r * std::min(t + w, FloatType(-0.0001)));
109-
c = FloatType(0.5) * (FloatType(1) - r) * (w - t) / r;
115+
const auto temp = FloatType(0.5) * (FloatType(1) - r) / r;
116+
a = temp / std::min(t + w, FloatType(-0.0001));
117+
c = temp * (w - t);
110118
}
111119
};
112120

@@ -116,7 +124,8 @@ namespace zldsp::compressor {
116124
std::atomic<FloatType> threshold_{-18}, ratio_{2};
117125
std::atomic<FloatType> knee_w_{FloatType(0.25)}, curve_{0};
118126
FloatType low_th_{0}, high_th_{0};
119-
std::array<FloatType, 3> para_mid_g0_, para_high_g0_;
127+
std::array<FloatType, 3> para_mid_g0_{}, para_high_g0_{};
128+
std::array<FloatType, 2> para_over_g0_{};
120129
std::atomic<bool> to_interpolate_{true};
121130

122131
void interpolate() {
@@ -131,30 +140,42 @@ namespace zldsp::compressor {
131140
const auto a1 = -low_th_;
132141
para_mid_g0_[0] = a0;
133142
const auto a0a1 = a0 * a1;
134-
if (OutputDiff) {
143+
if constexpr (OutputDiff) {
135144
para_mid_g0_[1] = FloatType(2) * a0a1;
136145
} else {
137146
para_mid_g0_[1] = FloatType(2) * a0a1 + FloatType(1);
138147
}
139148
para_mid_g0_[2] = a0a1 * a1;
140-
}
141-
if (current_curve >= FloatType(0)) {
142-
const auto alpha = FloatType(1) - current_curve, beta = current_curve;
143-
linear_curve_.setPara(current_threshold, current_ratio, current_knee_w);
144-
down_curve_.setPara(current_threshold, current_ratio, current_knee_w);
145-
para_high_g0_[2] = alpha * linear_curve_.c + beta * down_curve_.c;
146-
para_high_g0_[1] = alpha * linear_curve_.b + beta * down_curve_.b;
147-
para_high_g0_[0] = beta * down_curve_.a;
148-
} else {
149-
const auto alpha = FloatType(1) + current_curve, beta = -current_curve;
150-
linear_curve_.setPara(current_threshold, current_ratio, current_knee_w);
151-
up_curve_.setPara(current_threshold, current_ratio, current_knee_w);
152-
para_high_g0_[2] = alpha * linear_curve_.c + beta * up_curve_.c;
153-
para_high_g0_[1] = alpha * linear_curve_.b + beta * up_curve_.b;
154-
para_high_g0_[0] = beta * up_curve_.a;
155-
}
156-
if (OutputDiff) {
157-
para_high_g0_[1] -= FloatType(1);
149+
} {
150+
if (current_curve >= FloatType(0)) {
151+
const auto alpha = FloatType(1) - current_curve, beta = current_curve;
152+
linear_curve_.setPara(current_threshold, current_ratio, current_knee_w);
153+
down_curve_.setPara(current_threshold, current_ratio, current_knee_w);
154+
para_high_g0_[2] = alpha * linear_curve_.c + beta * down_curve_.c;
155+
para_high_g0_[1] = alpha * linear_curve_.b + beta * down_curve_.b;
156+
para_high_g0_[0] = beta * down_curve_.a;
157+
} else {
158+
const auto alpha = FloatType(1) + current_curve, beta = -current_curve;
159+
linear_curve_.setPara(current_threshold, current_ratio, current_knee_w);
160+
up_curve_.setPara(current_threshold, current_ratio, current_knee_w);
161+
para_high_g0_[2] = alpha * linear_curve_.c + beta * up_curve_.c;
162+
para_high_g0_[1] = alpha * linear_curve_.b + beta * up_curve_.b;
163+
para_high_g0_[0] = beta * up_curve_.a;
164+
}
165+
if constexpr (OutputDiff) {
166+
para_high_g0_[1] -= FloatType(1);
167+
}
168+
} {
169+
if (high_th_ <= FloatType(0)) {
170+
para_over_g0_[0] = para_high_g0_[1];
171+
para_over_g0_[1] = para_high_g0_[2];
172+
} else {
173+
para_over_g0_[0] = linear_curve_.b;
174+
para_over_g0_[1] = linear_curve_.c;
175+
if constexpr (OutputDiff) {
176+
para_over_g0_[0] -= FloatType(1);
177+
}
178+
}
158179
}
159180
}
160181
};

0 commit comments

Comments
 (0)