From c36583d79448bcbbfbeccb228899da06cb6f9ce7 Mon Sep 17 00:00:00 2001 From: R1CH Date: Sat, 4 Feb 2023 08:27:15 +0100 Subject: [PATCH] obs-filters: Ensure gain is positive for upward compressor The gain should be positive for an upward compressor. Initially, the gain would be zeroe'd below -60 dB to ensure noise is not amplified by the upward compressor. This created a discontinuity at -60 dB since just above -60 dB, the audio is boosted by +20 dB (at default settings). This was fixed in commit 50db097 which decreased smoothly the gain so that it's 0 dB at -60 dB. However that commit forgot to limit the gain decrease which was negative below -60 dB. This is fixed by the current commit. Additionally initialization allowed -inf gain. We hard limit to positive gains only as they should be for an upward compressor. The bugfix was found by R1ch and put in form by pkv. Co-authored-by: pkv Signed-off-by: pkv --- plugins/obs-filters/expander-filter.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/plugins/obs-filters/expander-filter.c b/plugins/obs-filters/expander-filter.c index be3ca9fbd..e162d4426 100644 --- a/plugins/obs-filters/expander-filter.c +++ b/plugins/obs-filters/expander-filter.c @@ -360,12 +360,15 @@ static inline void process_sample(size_t idx, float *samples, float *env_buf, float diff = threshold - env_db; if (is_upwcomp && env_db <= (threshold - 60.0f) / 2) - diff = env_db + 60.0f; + diff = env_db + 60.0f > 0 ? env_db + 60.0f : 0.0f; float gain = 0.0f; + float prev_gain = 0.0f; // Note that the gain is always >= 0 for the upward compressor // but is always <=0 for the expander. if (is_upwcomp) { + prev_gain = idx > 0 ? fmaxf(gain_db[idx - 1], 0) + : fmaxf(channel_gain, 0); // gain above knee (included for clarity): if (env_db >= threshold + knee / 2) gain = 0.0f; @@ -377,9 +380,9 @@ static inline void process_sample(size_t idx, float *samples, float *env_buf, threshold + knee / 2 > env_db) gain = slope * powf(diff + knee / 2, 2) / (2.0f * knee); } else { + prev_gain = idx > 0 ? gain_db[idx - 1] : channel_gain; gain = diff > 0.0f ? fmaxf(slope * diff, -60.0f) : 0.0f; } - float prev_gain = idx > 0 ? gain_db[idx - 1] : channel_gain; /* --------------------------------- */ /* ballistics (attack/release) */